在云代码中使用 javascript 进行嵌套查询 (Parse.com)

2023-11-21

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

我希望能够做类似的事情

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");
}   
});

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


第二个查询是异步的,因此将其包装在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 method.

取自他们文档的示例:

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

在云代码中使用 javascript 进行嵌套查询 (Parse.com) 的相关文章

随机推荐