Google数据存储中的节点分页 [英] Node pagination in google datastore
问题描述
我在使用Google数据存储区分页时遇到问题。我有一个没有限制的查询有几百个结果。我想检索5,发回给用户,如果用户想要更多,他们会检索下5个。
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.
按照文档我创建查询:
var query = datastore.createQuery('ResultsKind').filter('name', 'bobby').limit(5).autoPaginate(false);
然后我运行这个查询来得到前5个结果:
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;
}
};
问题是正在打印 res =
无限次,在控制台中没有结果。我不确定我做错了什么。我想要发生的是。
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.
我一直在寻找这份文件: http://googlecloudplatform.github.io/gcloud-node/#/docs/v0.30.2/数据存储/查询?method = autoPaginate 。
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?
推荐答案
在您的回调中,您将直接在 console.log
后重新运行查询:
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
}
这实质上与 autoPaginate(true)
的确如此。相反,您应该删除该行,缓存 nextQuery
,然后运行您删除的同一行,以在用户请求时获取下一批结果。
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.
这篇关于Google数据存储中的节点分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!