Node pagination in google datastore

心已入冬 提交于 2019-12-10 15:35:29

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/36534190/node-pagination-in-google-datastore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!