Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stored procedure azure Cosmos DB returns empty collection

I tried to create a stored procedure using the sample sp creation code from Azure docs, but i couldn't fetch the collection details. It always returns null.

Stored Procedure

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

    // 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.');
}

The console shows only this. Console output

the results shows no doc found because of not getting collection.I have passed the partition key at time of execution via explorer.

like image 689
Sanjeev S Avatar asked Apr 22 '19 04:04

Sanjeev S


1 Answers

I had a similar issue. I think the Azure portal doesn't execute stored procedures properly when the partition key is not a string.

In my case I had a partitionKey that is a number. When I executed the stored procedure via the portal I always got an empty resultSet, even though I had documents in my database. When I changed the structure a little, and made my partitionKey a string, the stored procedure worked fine.

like image 136
willem Avatar answered Sep 28 '22 00:09

willem