在云代码中使用 javascript 嵌套查询 (Parse.com) [英] Nested queries using javascript in cloud code (Parse.com)

查看:18
本文介绍了在云代码中使用 javascript 嵌套查询 (Parse.com)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to do nested queries in cloud code?

我希望能够做类似的事情

I want to be able to do something like

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

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

I tried doing this, but the inner query never executes. Why?

推荐答案

第二个查询是异步的,因此将其包装在 for 中将不起作用.

The second query is asynchronous so wrapping it in a for won't work.

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

response.success gets triggered before the second query finished so you are returning before actually waiting for the results. I would tell you to move response.success inside of the second success callback of query2 but that won't work either since you are running an async operation synchronously inside a for so you will send the response in the first success.

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

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

I'm not sure what you are trying to accomplish here but you'll have to instantiate a new Query for every call if you want to make as many queries as results.

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

Again, I'm not sure what is exactly you are trying to do but here is a way you can do something like that:

    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 秒,如果您有很多 结果,这可能会非常慢.顺便说一句,看看 Parse 的 matchesQuery 方法.

Bar in mind that Parse Cloud Code let you run code for aprox 5/7 seconds and this might be very slow if you have a lot of results. Btw, take a look a Parse's matchesQuery method.

从他们的文档中获取的示例:

Example taken from their docs:

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

这篇关于在云代码中使用 javascript 嵌套查询 (Parse.com)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆