6
votes

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.

3
I have the same issue... will comment if I figure it out...willem

3 Answers

1
votes

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.

0
votes

Did you create the ToDoList Database with the Items Collection? Yo can do this from the Quick start blade in the Azure portal.

Deploy Collection from Quick start blade

And then create an SP to run against that collection. There is no partition key required, so no additional params are required (leave blank).

Make sure you have a Collection created.

The Collection is created without any documents. You may choose to add documents via the Query Explorer blade or via the sample ToDoList App that is available via the Quick start blade.

0
votes

You are debugging in a wrong way.

It is perfectly fine to see "{\"spatial\":{}}" in your console log, even if the collection has items. Why? well because that is a property of that object.

So regarding what you said:

the results shows no doc found because of not getting collection

is false. I have the same console log text, but I have items in my collection.

I have 2 scenarios for why your stored procedure return no items:

  1. I had the same issue trying on azure portal UI(in browser) and for my surprise I had to insert an item without the KEY in order that my stored procedure to see it.
  2. On code you specify the partition as a string ie. new PartitionKey("/UserId") instead of your object ie. new PartitionKey(stock.UserId)