Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

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

I have a stored procedure which does the update a property value of a document in cosmos-db but always end up with this error. "Requests originating from scripts cannot reference partition keys other than the one for which client request was submitted."

The code works well if no partition key for the collection but if there is any partition key for the collection then it failed. Here the partition key is FlowName, I'm trying to update the value of the field of FlowName which is a partition key.

function updateSproc(flowName, newFlowName) {

    var collection = getContext().getCollection();
    var collectionLink = collection.getSelfLink();
    var response = getContext().getResponse();
    var responseBody = {
        newFlowName :newFlowName,
        oldFlowName:flowName,
        replaced:0,
        continuation: true
    };

    // Validate input.
    if (!flowName) {
        throw new Error("The flowName value is undefined or null.");
    }

    if (!newFlowName) {
        throw new Error("The replace value of flowName is undefined or null.");
    }

    tryQueryAndUpdate();
    function tryQueryAndUpdate(continuation) {

        var query = {query: "select * from root r where r.FlowName = @flowName", parameters: [{name: "@flowName", value: flowName}]};
        var requestOptions = {continuation: continuation};

        var isAccepted = collection.queryDocuments(collectionLink, query, requestOptions, function (err, documents, responseOptions) {

            if (err) {
                throw err;
            }

            if (documents.length > 0) {
                tryUpdate(documents);
            } else if (responseOptions.continuation) {
                tryQueryAndUpdate(responseOptions.continuation);
            } else {
                responseBody.continuation = false;
                response.setBody(responseBody);
            }
        });
        if (!isAccepted) {
            response.setBody(responseBody);
        }
    }

    function tryUpdate(documents) {
        if (documents.length > 0) {
            documents[0].FlowName = newFlowName;
            var isAccepted = collection.replaceDocument(documents[0]._self, documents[0], function (err, responseOptions) {

                if (err) throw err;

                responseBody.replaced++;
                documents.shift();
                tryUpdate(documents);
            });

            if (!isAccepted) {
                response.setBody(responseBody);
            }
        } else {

            // If the document array is empty, query for more documents.
            tryQueryAndUpdate();        
        }
    }
}

Can anyone assist here?

Comments