0
votes

I modify my original stored proc to show the issue. I somehow struggle to get the document(s) back from the collection. Now, I just try to get the document by id, and it still fails to do so. I always get the "True error" message back which means no document. However, when I run the query in the query window, the document is returned without any issue. I really have no idea what the issue can be. Below is my code. Any help appreciated.

function removeDisconnectedUserFromAllRooms(connectionID){
var collection = getContext().getCollection();
var collectionLink = collection.getSelfLink();
var response = getContext().getResponse();
var nodesBatch = [];

if (!connectionID) throw new Error("The id is undefined or null.");

var query = {query: "select * from rooms where rooms.RoomID = @connectionID", parameters: [{name: "@connectionID", value: connectionID}]};

var isAccepted = collection.queryDocuments(collectionLink, query, function (err, documents, options) {

if (err) throw err;

 if (documents.length > 0) {
           response.setBody(documents.length);
        } 
        else if (options.continuation){
             response.setBody("has token");
        }
        else {
            // Else a document with the given id does not exist..
            response.setBody("true error");
        }
    });
}
if(!isAccepted) {
throw new Error("The stored procedure timed out.");
}
}
1

1 Answers

0
votes

It seems that there is some syntaxError in your code. We could test the stored proc from the Azure portal, when use the code you mentioned I got following error info.

enter image description here

Please have a try use to the following code to test again, it works on my side.

function removeDisconnectedUserFromAllRooms(connectionID){
var collection = getContext().getCollection();
var collectionLink = collection.getSelfLink();
var response = getContext().getResponse();
var nodesBatch = [];

if (!id) throw new Error("The id is undefined or null.");

var query = {query: "select * from rooms where rooms.RoomId= @connectionID", parameters: [{name: "@connectionID", value: connectionID}]};

var isAccepted = collection.queryDocuments(collectionLink, query, function (err, documents, options) {

if (err) throw err;

 if (documents.length > 0) {
           response.setBody(documents.length);
        } 
        else if (options.continuation){
             response.setBody("has token");
        }
        else {
            // Else a document with the given id does not exist..
            response.setBody("true error");
        }
    });
    if(!isAccepted) {
throw new Error("The stored procedure timed out.");
}
}

Note: field name it is case sensitive in the sql query in the documentdb.

enter image description here