1

我在使用Google數據存儲區分頁時遇到問題。我有一個沒有限制的查詢有幾百個結果。我想找回5,送他們回給用戶,如果用戶想要更多,他們將檢索下一個5Google數據存儲區中的節點分頁

繼文檔創建查詢:

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

我該如何完成這個簡單的分頁?

回答

1

從您的回調中,您要重新運行該查詢的console.log後直接:

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

這實質上是在做同樣的事情autoPaginate(true)一樣。相反,您應該刪除該行,緩存nextQuery,然後運行您刪除的同一行,以在用戶請求時獲取下一批結果。

+0

謝謝它的工作原理! – user2924127

相關問題