3

I am trying to create a stored procedure that my app can call to run operations on a SQL cosmos DB in azure. I'm developing this using the Azure portal.

I generated a sample procedure, which is shown below.

// SAMPLE STORED PROCEDURE
function sample(prefix) {
    var collection = getContext().getCollection();

    // Query documents and take 1st item.
    var isAccepted = collection.queryDocuments(
        collection.getSelfLink(),
        'SELECT * FROM root r',
    function (err, feed, options) {
        if (err) throw err;

        // Check the feed and if empty, set the body to 'no docs found', 
        // else take 1st element from feed
        if (!feed || !feed.length) {
            var response = getContext().getResponse();
            response.setBody('no docs found');
        }
        else {
            var response = getContext().getResponse();
            var body = { prefix: prefix, feed: feed[0] };
            response.setBody(JSON.stringify(body));
        }
    });

    if (!isAccepted) throw new Error('The query was not accepted by the server.');
}

When I run this, I get no results, but I created a single document in the documents section under the same tree as this stored procedure. When I run the stored procedure, I get no results. What am I doing wrong?

jaredad7
  • 998
  • 2
  • 11
  • 33

1 Answers1

1

Your stored procedure code is totally as same as the sample code on the portal. It works fine without any issues.

enter image description here

Maybe you could check following points:

1.check whether the document is under the same collection with stored procedure.(just close other unrelated tags and check again!)

2.check if the sql select * from root r could grab the data on the portal.

3.try to create a new collection and a new stored procedure then check if the situation is constant.

4.try to use console.log() to debug and follow the stored procedure, please refer to my previous case: How to debug Azure Cosmos DB Stored Procedures?


Just for summary, we also need to check if we miss the partition key when we execute query sql!

halfer
  • 19,824
  • 17
  • 99
  • 186
Jay Gong
  • 23,163
  • 2
  • 27
  • 32