3
votes

I have below stored procedure which is written to work on continuation token based mechanism for fetching documents from the documentDB collection:

  1. I am getting exception with query.
  2. I am trying to get all the documents using continuation token.

Is this the correct way?

function getOrdersByBranchNumber(branchNumber){
var context = getContext();
var collection = context.getCollection();
var link = collection.getSelfLink();
var response = context.getResponse();
var nodesBatch = [];
var continuationToken = true;
var responseSize = 0;

//validate inputs
if(!branchNumber || (typeof branchNumber != "string")){

return errorResponse(400, (!branchNumber) ? "branchNumber is Undefined":"String type is expected for branchNumber.");
}
var querySelect = "SELECT * from orders o WHERE o.branchNbr = '"+branchNumber+"' ";
var query = { query: querySelect};

getNodes(continuationToken);

function getNodes(continuationToken) {
var requestOptions = {
  continuation: continuationToken,
  pageSize: 90
};

var isAccepted = collection.queryDocuments(
    collection.getSelfLink(),
    query,
    requestOptions,
    function (err, feed, options) {
        var queryPageSize = JSON.stringify(feed).length;
    // DocumentDB has a response size limit of 1 MB.
    if (responseSize + queryPageSize < 1024 * 1024) {
      // Append query results to nodesBatch.
      nodesBatch = nodesBatch.concat(feed);

      // Keep track of the response size.
      responseSize += queryPageSize;

      if (responseOptions.continuation) {
        // If there is a continuation token... Run the query again to get the next page of results
        lastContinuationToken = responseOptions.continuation;
        getNodes(responseOptions.continuation);
      } else {
        // If there is no continutation token, we are done. Return the response.
        var feedData=JSON.stringify(nodesBatch);
        getContext().getResponse().setBody(feedData);
      }
    } else {
      // If the response size limit reached; run the script again with the lastContinuationToken as a script parameter.
        var feedData=JSON.stringify(nodesBatch);
        getContext().getResponse().setBody(feedData);
    }
    });

if (!isAccepted){
    return errorResponse(400, "The query was not accepted by the server.");
}
}
function errorResponse(code,message){
    var errorObj = {};
    errorObj.code = code;
    errorObj.message = message;
    errorObj.date = getDateTime();
    return response.setBody(errorObj);
}

function getDateTime(){
    var currentdate = new Date();
    var dateTime = currentdate.getFullYear() + "-" +(currentdate.getMonth()+1)+ "-" +           currentdate.getDate()+ " "  +currentdate.getHours()+":"+currentdate.getMinutes()           +":"+currentdate.getSeconds();
    return dateTime;
} 
}

But I am seeing below mentioned error after saving and executing the stored procedure.

Any idea as to what is wrong in query?

Encountered exception while executing Javascript. Exception = Error: Invalid 
arguments for query.
Stack trace: Error: Invalid arguments for query.
at queryDocuments (getOrdersByBranchNumber.js:614:17)
at getNodes (getOrdersByBranchNumber.js:27:5)
at getOrdersByBranchNumber (getOrdersByBranchNumber.js:19:1)
at __docDbMain (getOrdersByBranchNumber.js:78:5)
at Global code (getOrdersByBranchNumber.js:1:2)
1

1 Answers

0
votes

Try removing the continuation item on the query options. I got this error when I had an invalid one specified.