azure - DocumentDB, How to work with continuationToken in a SP -


the next sp suppose run on collection , keep query next batch of documents (10 docs every batch). instead return same 10 documents every time.

function sample(prefix) { var continuations = [], ids = [],  context = getcontext(),      collection = context.getcollection(),      response = context.getresponse(); var queryoptions = { pagesize: 10, continuation: null };  (i = 0; < 10; i++) {     // user wish list actions     var query = "select * w",     accept = collection.querydocuments(collection.getselflink(), query, queryoptions, processmultiusers);     if (!accept) throw "unable read user's sessions"; } getcontext().getresponse().setbody(ids);   function processmultiusers(err, docs, options) {     if (err) throw new error("error: " + err.message);     if (docs == undefined || docs.length == 0) throw new error("warning: users not exists");      (j = 0; j < docs.length; j++) {         ids.push(docs[j].userid);     }     queryoptions.continuation = options.continuation;     continuations.push(options.continuation); }} 

in script wrote, execution of queries done synchronously , queued same initial continuation token, null. instead, need take token first query , queue next , continue.

the below sample should achieve looking for

function sample(continuationtoken) {     var collection = getcontext().getcollection();     var maxresult = 10;     var documentsprocessed = 0;     var ids = [];     var filterquery = "select * w";      tryquery(continuationtoken);      function tryquery(nextcontinuationtoken) {         var responseoptions = { continuation: nextcontinuationtoken, pagesize: maxresult };         if (documentsprocessed >= maxresult || !query(responseoptions)) {             setbody(nextcontinuationtoken);         }     }      function query(responseoptions) {         return (filterquery && filterquery.length) ?             collection.querydocuments(collection.getselflink(), filterquery, responseoptions, onreaddocuments) :             collection.readdocuments(collection.getselflink(), responseoptions, onreaddocuments);     }      function onreaddocuments(err, docfeed, responseoptions) {         if (err) {             throw 'error while reading document: ' + err;         }          documentsprocessed += docfeed.length;          (var = 0; < documentsprocessed; i++) {             ids.push(docfeed[i].userid);         }          if (responseoptions.continuation) {             tryquery(responseoptions.continuation);         } else {             setbody(null);         }     }      function setbody(continuationtoken) {         var body = { continuationtoken: continuationtoken, documentsprocessed: documentsprocessed, ids: ids };         getcontext().getresponse().setbody(body);     } } 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -