0
votes

How to execute an sample stored procedure in azure server.

I am using cosmos db emulator and whenever i try to execute a sample sp i get this error

Requests originating from scripts cannot reference partition keys other than the one for which client request was submitted.

Stored procedure

function createToDoItem(itemToCreate) {
    var context = getContext();
    var container = context.getCollection();
    console.log("success");
    var itemToCreate={
        "Id": null,
        "UserAccountID": "1742",
        "FirstName": "Sanjeev",
        "LastName": "S",
        "Phone": "12345678",
        "Location": "",
        "StreetAddress": "vcbgvbvc",
       };

itemToCreate.partitionKey = "UserAccountID";

    var accepted = container.createDocument(container.getSelfLink(),
        itemToCreate,
        function (err, itemCreated) {
            if (err) throw new Error('Error test' + err.message);
            context.getResponse().setBody(itemCreated.id)
        });
    if (!accepted) return;
}

the sample stored procedure also fails to get the desired results. linking the question here

2
When you're executing the stored procedure, what's the value of PartitionKey you're passing?Gaurav Mantri
@GauravMantri UserAccountIDSanjeev S
Sorry, I meant the value of PartitionKey attribute.Gaurav Mantri
attribute?. I didn't get youSanjeev S
If you look at the document you're trying to create above, which attribute in that document is the PartitionKey attribute? Is it UserAccountID?Gaurav Mantri

2 Answers

2
votes

Sanjeev S,based on the issue message:

Requests originating from scripts cannot reference partition keys other than the one for which client request was submitted.

It claims that your partition key in your document needs to match the partition key setting of your collection.

For example,your collection's partition key is /name,

enter image description here

Then you need to exclude the name property in your inserted document and provide the partition key like this when you execute the SP.

enter image description here

Output:

enter image description here

0
votes

You need to pass the the partition key to your document inside the stored procedure as you're dealing with a dynamic JS object.

doc.partitionKey = 'some_partition_key'