2
votes

I'm trying to create a stored procedure in Azure Cosmos DB using node.js. Creating the stored procedure is successful, but when trying to execute it (from javascript/nodejs), it returns no documents.

First, I define the stored procedure and then register it in Cosmos DB:


var DocumentDBClient = require('documentdb').DocumentClient;

var host = "our-hostname";  // Add your endpoint
var masterKey = "our-master-key"; // Add the masterkey of the endpoint

var client = new DocumentDBClient(host, {masterKey: masterKey});

var powerPlantDataReadingStoredProc = {
    id: "GetPowerPlantDataReadingsSproc",
    serverScript: function() {           
        var context = getContext();           
        var collection = context.getCollection();   
        var request = context.getRequest();
        var response = context.getResponse();

        var isAccepted = collection.queryDocuments(collection.getSelfLink(),
                    'SELECT TOP 1 * FROM c',
                    function(err, feed, options) {
                        response.setBody(JSON.stringify(feed));
                    });

        if(!isAccepted) throw new Error("The query was not acceted by the server");
     }
};

// Register stored proc
var createdStoredProc;

client.createStoredProcedure('dbs/PowerPlantDataReadings/colls/DataReadings', powerPlantDataReadingStoredProc, function(err, sproc) {
    createdStoredProc = sproc;
});  

And the code I'm using for executing it:

client.executeStoredProcedure("dbs/PowerPlantDataReadings/colls/DataReadings/sprocs/GetPowerPlantDataReadingsSproc", null,
    { partitionKey: "datareadings" },
    function(err, results, responseHeaders) {
        if(err) {
            console.log("Error: ");
            console.log(JSON.stringify(err));
        }

        if(responseHeaders) {
            console.log("Headers: ");
            console.log(JSON.stringify(responseHeaders));
        }

        console.log("Results: " + results);

        if(results) {
            console.log("Results: ");
            console.log(JSON.stringify(results));
        }
    });

I'm getting no errors in the console. Just an empty array. Also, there are documents in the collection ;-)

Does anyone know what I'm missing here?

Thanks in advance.

EDIT:

Getting these response headers from the sproc:

Headers: {"cache-control":"no-store, no-cache","pragma":"no-cache","transfer-encoding":"chunked","content-type":"application/json","server":"Microsoft-HTTPAPI/2.0","strict-transport-security":"max-age=31536000","x-ms-last-state-change-utc":"Tue, 05 Sep 2017 08:01:38.741 GMT","x-ms-schemaversion":"1.3","x-ms-alt-content-path":"dbs/PowerPlantDataReadings/colls/DataReadings/sprocs/GetPowerPlantDataReadingsSproc","x-ms-content-path":"cjFwAKg0WQA=","x-ms-quorum-acked-lsn":"25347","x-ms-current-write-quorum":"3","x-ms-current-replica-set-size":"4","x-ms-xp-role":"1","x-ms-request-charge":"6.12","x-ms-serviceversion":"version=1.14.89.5","x-ms-activity-id":"604b8d09-ad08-427a-8dee-1b7862e0e092","x-ms-session-token":"0:25347","x-ms-gatewayversion":"version=1.14.89.5","date":"Tue, 05 Sep 2017 09:38:32 GMT","x-ms-throttle-retry-count":0,"x-ms-throttle-retry-wait-time-ms":0}

Results: []

Also, when making the query in the query editor on Azure Portal, I do get a result as expected:

enter image description here

My partition key is as follows:

enter image description here

1
Shouldn't serverScript attribute name be body in your code?Gaurav Mantri
@GauravMantri hmm, not according to this article, it seems? :-) docs.microsoft.com/en-us/azure/cosmos-db/programmingBo Mortensen
Mine was based on this: docs.microsoft.com/en-us/rest/api/documentdb/…. In fact we're using Node SDK and we use body attribute to specify function body.Gaurav Mantri
@GauravMantri thanks! Just tried to name the attribute body instead of serverScript, but it made no difference :-/ No errors at all, but no results eitherBo Mortensen
Thing is, if I just do response.setBody("Hello World") it returns fine, so maybe it's because of the query logicBo Mortensen

1 Answers

0
votes

Did you try setting the Partition Key to the actual value? this worked for me. I was giving my partition key name to the stored procedure input but I actually gave it a partition key VALUE (because stored procedures operate on specific partitions) and it worked.

EDIT:

Just looked at the comments and Gaurav seemed to have suggested this approach as well