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:
My partition key is as follows:
serverScript
attribute name bebody
in your code? – Gaurav Mantribody
attribute to specify function body. – Gaurav Mantribody
instead ofserverScript
, but it made no difference :-/ No errors at all, but no results either – Bo Mortensenresponse.setBody("Hello World")
it returns fine, so maybe it's because of the query logic – Bo Mortensen