1
votes

I am trying to write a procedure that logs that an Item was retrieved when the procedure is used. Right now, I can retrieve the items but I am just stuck on adding a single key and value to the item as I loop through the collection:

Here is my code:

function example(prefix) {
var collection = getContext().getCollection();
collection.chain().filter(function (doc) {
    return doc.textStatus = "delivered";
}).map(function (doc) {
    doc.note = "test";
    var isAccepted = collection.replaceDocument(doc._self, doc, function (err) {
        if (err) throw err;
    });
    if (!isAccepted) throw new Error("replaceDocument(metaItem) returned false.");

}).value();}

This is giving me this message:

Message: {"Errors":["Encountered exception while executing function. Exception = Error: {"Errors":["Requests originating from scripts cannot reference partition keys other than the one for which client request was submitted."]}

I have also tried setting the partition key of the doc (e.g. doc.partitionKey = "sid"), though I would assume it is set as the docs are being pulled from the collect.

Update

I thought that is code was sending docs to map function but when I checked again, it was not. I thought that my partition key was "/sid" or "sid" and entered those as strings for parameters of the stored procedure. This was after adding === and sid on filters. This produced no errors, but I could not log or change any docs.

Showing top of collection list

Showing where partition key is entered for stored procedure

1

1 Answers

1
votes

Please change return doc.textStatus = "delivered"; to return doc.textStatus == "delivered" && doc.partitionKey == "sid"; to have a try. It works for me.

enter image description here

Update

When you want to execute your SP, you need to pass your partition key value not your partition key path. According to your screenshot, your partition key path is /sid and your partition key value is undefined.

So your SP should be like this:

function example(partition_key_value) {
    var collection = getContext().getCollection();
    if(!partition_key_value){
        partition_key_value = undefined;
    }
    collection.chain().filter(function (doc) {
        return doc.textStatus == "delivered" && doc.sid== partition_key_value;
    }).map(function (doc) {
        doc.note = "test";
        var isAccepted = collection.replaceDocument(doc._self, doc,function (err) {
            if (err) throw err;
        });
    if (!isAccepted) throw new Error("replaceDocument(metaItem) returned false.");
    }).value();
}

And execute your SP like this:

enter image description here