Google数据存储中的节点分页

我在使用Google数据存储区分页时遇到问题。 我有一个没有限制的查询有几百个结果。 我想检索5,发回给用户,如果用户想要更多,他们会检索下一个5。

按照文档我创build查询:

var query = datastore.createQuery('ResultsKind').filter('name', 'bobby').limit(5).autoPaginate(false); 

然后我运行这个查询来得到前5个结果:

 datastore.runQuery(query, callback); 

这是callback函数:

 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 =正在无限次打印,在控制台中没有任何结果。 我不确定我做错了什么。 我想要发生的是。

 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/datastore/query ?method= autoPaginate 。

我怎样才能完成这个简单的分页?

在您的callback中,您将直接在console.log后重新运行查询:

 if (nextQuery) { console.log('res = ' + entities); datastore.runQuery(nextQuery, callback); // <-- Here } 

这本质上和autoPaginate(true)一样。 相反,您应该删除该行,cachingnextQuery ,然后运行您删除的同一行,以在用户请求时获取下一批结果。