I am having trouble doing the pagination with Google Datastore. I have a query which without the limit has a few hundred results. I would like to retrieve 5, send them back to the user, if the user wants more they would retrieve the next 5.
Following the docs I create the query:
var query = datastore.createQuery('ResultsKind').filter('name', 'bobby').limit(5).autoPaginate(false);
I then run this query to get the first 5 results:
datastore.runQuery(query, callback);
This is the callback function:
function callback(err, entities, nextQuery, apiResponse) {
if (err) {
// An error occurred while running the query.
console.log('err ' + err);
return;
}
if (nextQuery) {
console.log('res = ' + entities);
datastore.runQuery(nextQuery, callback);
} else {
// No more results exist.
console.log('no more results');
return;
}
};
The problem is res =
is being printed infinity times with no results in the console. I am unsure what I am doing wrong. What I would like to happen is.
1) I create the initial query.
2) I run the query.
3) I get the first 5 results.
4) I pass these results + the nextquery object to the user.
5) If the user wants more results the pass me back the nextQuery and I run this query and get the next 5 results and so on.
I have been looking at this doc:http://googlecloudplatform.github.io/gcloud-node/#/docs/v0.30.2/datastore/query?method=autoPaginate.
How can I accomplish this simple pagination?
From within your callback, you are re-running the query directly after the console.log
:
if (nextQuery) {
console.log('res = ' + entities);
datastore.runQuery(nextQuery, callback); // <-- Here
}
This is essentially doing the same thing as autoPaginate(true)
does. Instead, you should remove that line, cache nextQuery
, then run the same line that you removed to get the next batch of results when the user asks to.