如何在云代码中使用Parse promise进行循环查询

How to make queries in loop using Parse promises in cloud code?

本文关键字:promise Parse 查询 循环 代码      更新时间:2023-09-26

接收到的响应始终是一个空数组。for循环中的内部stands_query永远不会被执行。此外,我想知道如果看台查询中再次出现内部查询,那么我该如何实现这一点。展位类别展位类别代码如下:

var final_list = [];
query.find().then(function(stadiums){
    _.each(stadiums,function(stadium){
        var stands_query = new Parse.Query(“Stands");
        stands_query.equalTo(“stdId”,stadium.get(“stdId"));
        var promise =  stands_query.find().then(function(stands){
             _.each(stands,function(stand){
                  var jsonObject = {
                      “stdId": stand.get(“stdId").id,
                   }
                   final_list.push(jsonObject);
             });
             return null;
        },function(error){
             return response.error(error);
        });
        return promise;
       });
 }).then(function(){
    response.success(final_list);
 });

您的第一个.then没有返回任何内容。我会分解你的代码,这样你就可以看到:

query.find().then(function(stadiums){   //anonymous function 1
  _.each(stadiums,function(stadium){    //anonymous function 2
    return "foo" //this returns "foo" as a result of anonymous function 2.
  });
  //Nothing explicitly returned from function 1!
}).then(function(){
  response.success(final_list);
});

缺少显式返回语句的函数将返回undefined。然后,您的代码在任何内部承诺解析之前执行"response.success"。

相反,您可以创建一个内部承诺数组,等待Parse.Promise.when:

query.find().then(function(stadiums){
  var promises = [];
  _.each(stadiums,function(stadium){
    var promise = stands_query.find().then(...)
    promises.push(promise);
  });
  //if returning another promise, the ".then" won't execute until it completes.
  return Parse.Promise.when(promises); 
}).then(function(){
  response.success(final_list);
});

尽管如此,根据数据集的大小,您可能会遇到超时问题。请考虑重写查询,以便使用关系查询来查询属于StadiumStands


更新

既然您已经用字段更新了问题,那么您的行stands_query.equalTo(“stdId”,stadium.get(“stdId"));似乎有两个错误,并且永远不会返回结果。应该是stands_query.equalTo(“stadiumId”,stadium);

我们有很多体育场,每个体育场都有很多看台。体育场和看台之间的关系在数据中由看台类上名为"stadiumId"的指针列表示。

在评论中,功能目标表述得非常简单:一个JSON的支架数组。这需要一个单一的查询,根本没有循环:

function allTheStands() {
    var query = new Parse.Query("Stands");
    query.include("stadiumId");
    return query.find().then(function(stands) {
        return JSON.stringify(stands);
    });
}
// call it like this:
allTheStands().then(function(jsonStands) {
    // jsonStands is all of the stands represented as son
});

编辑

对于相同的结果,一种更迂回的方法是在查询中不包括stadiumId,而是在stands查询完成后进行提取。

(这只是@adamdport给出的一种特定形式的建议,考虑到你的数据细节。如果你觉得这很有用,你应该相信他的回答)。

// adding underscorejs for handling arrays and other utils
var _ = require('underscore');
function allTheStands() {
    var stands;
    var query = new Parse.Query("Stands");
    return query.find().then(function(result) {
        stands = result;
        // we're not done yet, because we need to fetch each stand's stadium
        var promises = _.map(stands, function(stand) {
            return stand.get("stadiumId").fetch().then(function(stadium) {
                stand.set("stadiumId", stadium);
            });
        });
        // as adamdport suggests, the crux of the looping answer is to use Promise.when()
        return Parse.Promise.when(promises);
    }).then(function() {
        return JSON.stringify(stands);
    });
}