4

Google Datastore でページネーションを行うのに問題があります。制限なしで数百の結果を持つクエリがあります。ユーザーがさらに必要な場合は、次の 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

この単純なページネーションをどのように達成できますか?

4

1 に答える 1

3

コールバック内から、次の直後にクエリを再実行していますconsole.log

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

これは、基本的には同じことを行ってautoPaginate(true)います。代わりに、その行 cache を削除してから、削除しnextQueryたのと同じ行を実行して、ユーザーが求めたときに結果の次のバッチを取得する必要があります。

于 2016-04-10T21:59:22.940 に答える