1

I am using the following code to get the list of documents using pagination. The code is working fine. But how do I find the continuation token if I want to send it from the client for pagination.

function queryCollectionPaging() {  
return new Promise((resolve, reject) => {
    function executeNextWithRetry(iterator, callback) {         
        iterator.executeNext(function (err, results, responseHeaders) {
            if (err) {
                return callback(err, null);
            }
            else {
                documents = documents.concat(results);
                if (iterator.hasMoreResults()) {
                    executeNextWithRetry(iterator, callback);
                }
                else {
                    callback();
                }
            }
        });
    }

    let options = {
        maxItemCount: 1,
        enableCrossPartitionQuery: true
    };

    let documents = []
    let iterator = client.queryDocuments( collectionUrl, 'SELECT r.partitionkey, r.documentid, r._ts FROM root r WHERE r.partitionkey in ("user1", "user2") ORDER BY r._ts', options);

    executeNextWithRetry(iterator, function (err, result) {
        if (err) {
            reject(err)
        }
        else {
            console.log(documents);
            resolve(documents)
        }
    });
});

};

Amlan
  • 241
  • 6
  • 13

1 Answers1

1

You could find the continuation token in the responseHeaders parameter, please try to use responseHeaders ['x-ms-continuation'] to grab it.

Such as :

continuationToken = responseHeaders ['x-ms-continuation'];

Then you could pass the token as a parameter to the execute method.

let options = {
        maxItemCount: 1,
        enableCrossPartitionQuery: true,
        continuation : continuationToken
    };

If the continuationToken is null, it means no more results.

You could refer to my previous case: How to get & set Cosmos Db continuation token in javascript.

halfer
  • 19,824
  • 17
  • 99
  • 186
Jay Gong
  • 23,163
  • 2
  • 27
  • 32