9

是否可以在云代码中进行嵌套查询?

我希望能够做类似的事情

var adList = [];
var query2 = new Parse.Query("QR");
var query = new Parse.Query("Campaigns");
query.equalTo("isFeatured", true);

query.find({
success: function(results)  {
    for (var i=0; i<results.length; i++){
        var ad = [];
        ad.push(results[i].get("Type"));    //Adds "Type" to the ad array
        ad.push(results[i].id);     //gets the objectID and appends it to the arrayy

          //second INNER QUERY
        query2.equalTo("abc", true);
        adList.push(ad);
        query2.first({
            success: function(results){
                adList.push(5);
            },
            error: function(){
                response.error("inner fail");
            }
        });

    }

    response.success(adList);  //adds all ad arrays to adList array
},
error: function(){
    response.error("failed");
}   
});

我试过这样做,但内部查询永远不会执行。为什么?

4

3 回答 3

14

第二个查询是异步的,因此将其包装在 a 中是for行不通的。

response.success在第二个查询完成之前触发,因此您在实际等待结果之前返回。我会告诉你移动response.success到第二个success回调的内部,query2但这也不起作用,因为你在 a 内同步运行异步操作,for所以你将在第一个回调中发送响应success

不要在循环内运行异步操作。它不起作用。

我不确定您要在这里完成什么,但是Query如果您想进行与结果一样多的查询,则必须为每个调用实例化一个新的。

同样,我不确定您到底要做什么,但这里有一种方法可以让您做类似的事情:

    var adList = [];
    var query = new Parse.Query("Campaigns");
    query.equalTo("isFeatured", true);

    query.find({
        success: function(results)  {
            var queries = [];

            for (var i=0; i<results.length; i++){
                var query2 = new Parse.Query("QR");
                query2.equalTo("abc",true);

                var ad = [];
                ad.push(results[i].get("Type"));
                ad.push(results[i].id);
                adList.push(ad);
                queries.push(query2);
            }

            var totalLength = results.length;

            function makeQueries(qs){
                qs.shift().first({
                    success: function(currentResult) {
                        // do stuff with currentResult
                        if(qs.length){
                            makeQueries(qs);
                        } else {
                            console.log('We successfully made ' + totalLength + ' queries')
                            // we are done with the queries
                            response.success(adList);
                        }
                    },
                    error: function() {
                        response.error('Error in inner queries nº' + totalLength - qs.length)
                    }
                });
            }
            makeQueries(queries);
        },
        error: function(){
            response.error("failed");
        }
    });

请记住 Parse Cloud Code 让您运行代码大约 5/7 秒,如果您有很多results. 顺便说一句,看看 Parse 的matchesQuery方法。

取自他们的文档的示例:

    var Post = Parse.Object.extend("Post");
    var Comment = Parse.Object.extend("Comment");
    var innerQuery = new Parse.Query(Post);
    innerQuery.exists("image");
    var query = new Parse.Query(Comment);
    query.matchesQuery("post", innerQuery);
    query.find({
      success: function(comments) {
        // comments now contains the comments for posts with images.
      }
    });        
于 2013-11-05T00:22:54.480 回答
1

我认为下面的代码可能对某人有所帮助,

var adList = [];
var query2 = new Parse.Query("QR");
var query = new Parse.Query("Campaigns");
query.equalTo("isFeatured", true);

query.find().then(function(results) {
    var _ = require('underscore');
    var promise = Parse.Promise.as();
    _.each(results, function(resultObj) { 
        var ad = [];
        ad.push(resultObj.get("Type"));    //Adds "Type" to the ad array
        ad.push(resultObj.id);     //gets the objectID and appends it to the arrayy

        //second INNER QUERY
        query2.equalTo("abc", true);
        adList.push(ad);
        return query2.first().then(function(results) {
            adList.push(5);
        });
    });
    return promise;
}).then(function() {
    response.success(adList);
}, function (error) {
    response.error("Error "+error.message);
});
于 2015-01-22T06:58:52.247 回答
0

我能够拼凑出以下答案。这对我有用。遵循 parse.com 并行承诺的文档指南。我创建了两个承诺数组。其中之一用作内部循环的承诺数组。另一个用作外部循环的承诺数组。

Parse.Cloud.define("getFriends", function (request, response) {
  var _ = require('underscore.js');
  var queryFields = request.params.queryFields;
  //var ClassToSearch = Parse.Object.extend("User");

  var promises = [];
  var promises2 = [];
  var FinalResult = [];
 var asyncFunc = function (userIDArray) {
    _.each(queryFields, function (queryField) {
      var query = new Parse.Query(Parse.User);
      query.equalTo("yourColumnName", queryField);
      promises.push(
        query.find().then(function (results) {
       _.each(results, function (resultObj) { 
        //nested query
        var ClassToSearch = Parse.Object.extend("AnotherColumn");
        var query2 = new Parse.Query(ClassToSearch);
        query2.equalTo("yourColumnName", resultObj);
        promises2.push(        
          query2.first().then(function (itinerary) {
          FinalResults.push(itinerary);
        }));

      });
      return Parse.Promise.when(promises2);
    })
          )
    });
    // Return a new promise that is resolved when all of the requests are done 
    return Parse.Promise.when(promises);
  };
  asyncFunc(queryFields).then(function () {
    console.log("all http requests were made");
    response.success(FinalResults);
  }, function (error) {
    console.log("there was some error");
  });
});
于 2015-10-04T05:43:44.933 回答