谷歌数据存储中的节点分页

2024-01-27

我在使用 Google Datastore 进行分页时遇到问题。我有一个查询,没有限制,有几百个结果。我想检索 5 个,将它们发送回用户,如果用户想要更多,他们会检索下 5 个。

根据文档,我创建了查询:

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

然后我运行此查询来获取前 5 个结果:

datastore.runQuery(query, 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 http://googlecloudplatform.github.io/gcloud-node/#/docs/v0.30.2/datastore/query?method=autoPaginate.

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


在您的回调中,您将在console.log:

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

这本质上是做同样的事情autoPaginate(true)做。相反,您应该删除该行,缓存nextQuery,然后运行您删除的同一行,以在用户要求时获取下一批结果。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

谷歌数据存储中的节点分页 的相关文章

随机推荐