AlexC
AlexC

Reputation: 37

Azure DocumentDB Stored Procedure in Node.js excute existing

I have been going around in circles with this. I'm trying to execute an existing stored procedure using the node js documentdb library.

var sproc = self.client.queryStoredProcedures(collection._self, "select * from root r WHERE r.id = 'helloWorld'");

self.client.executeStoredProcedure(sproc._self, function (err, res) {
    if(err){ 
        console.log(err);
    }else{
        console.log(res);`
    }
});

Not entirely sure queryStoredProcedures (Seems to be no async version of this) is the correct way of retrieving the uri for the store procedure, I haven't managed to get this to work. I'm also trying to avoid too many round trips to the database, but from what I gather I either hard code the store procedure's uri or have to make at least two requests just to execute the stored procedure.

Upvotes: 0

Views: 712

Answers (1)

Larry Maccherone
Larry Maccherone

Reputation: 9523

queryStoredProcedures (along with all query and read functions) return a QueryIterator rather than an actual result. The methods you call on the returned QueryIterator are async. So, following the approach of your example (minus error handling), you would do this:

var queryIterator = self.client.queryStoredProcedures(collection._self, "select * from root r WHERE r.id = 'helloWorld'");
queryIterator.toArray(function(err, result) {
    var sproc = result[0];
    self.client.executeStoredProcedure(sproc._self, function (err, res) {
        console.log(res);`
    });
});

However, since the introduction of id-based routing, you can short hand the above like this:

var sprocLink = "dbs/myDatabase/colls/myCollection/sprocs/helloWorld";
self.client.executeStoredProcedure(sprocLink, function (err, res) {
    console.log(res);`
});

Upvotes: 1

Related Questions