简体   繁体   English

为什么“解析云代码”中的此Promise无效?

[英]Why does this Promise in Parse Cloud Code not work?

The following Cloud Code function on Parse Server does not work as expected. Parse Server上的以下Cloud Code功能无法正常工作。

It seems to resolve too early so that not all chained promises of promise2 are executed. 这似乎太早,这样不是所有的链接承诺解决promise2被执行。 In particular I see some of the resultA.add(...); 我特别看到了一些resultA.add(...); not being executed. 没有被执行。

I have tried to re-arrange this several times without success. 我试图几次重新安排,但没有成功。

I am not sure if return Parse.Promise.resolve(); 我不确定是否return Parse.Promise.resolve(); is what I should return in the else clause or if it is something else or nothing. 是我应该在else子句中返回的else或者是其他内容或什么都不做的内容。

Can anyone spot something here? 有人可以在这里看到东西吗?

Parse.Cloud.define("aFunction", function(request, response) {

    var q = new Parse.Query("ClassA");    
    q.find()
    .then(
        function(resultsA) {

            var promise = new Parse.Promise.as();
            resultsA.forEach(function(resultA) {

                promise = promise
                .then(
                    function() {
                        var q = new Parse.Query("ClassB");
                        return q.first();
                    }
                )
                .then(
                    function(resultB) {

                        if (resultB != undefined) {
                            resultA.set(...);
                            return resultA.save();

                        } else {
                            resultA.set(...);
                            return resultA.save();
                        }
                    }
                )
                .then(
                    function() {

                        var q = new Parse.Query("ClassC");
                        return q.find()

                        .then(
                            function(resultsC) {
                                if (resultsC != undefined && resultsC.length > 0) {

                                    var promise2 = new Parse.Promise.as();
                                    resultsC.forEach(function(resultC) {
                                        promise2 = promise2
                                        .then(
                                            function() {
                                                resultA.add(...);
                                                return resultA.save();
                                            }
                                        );
                                    });
                                    return promise2;

                                } else {
                                    return Parse.Promise.resolve();
                                } 
                            }
                        );
                    }
                );
            });
            return promise;
        }
    )
    .then(
        function(result) {
            response.success("success");
        },
        function(error) {
            response.error(error.message);
        }
    );
});

Long code but I already stripped it down and wanted to show the actual structure. 长代码,但我已经精简了一下,想显示实际结构。 Thanks. 谢谢。

just checked your code. 刚刚检查了您的代码。 And i'm not sure if it's your type mistake , when you 'clean your code' for showing structure. 而且,当您“清理代码”以显示结构时,我不确定这是否是您的类型错误。

in your loop: chats.forEach(function(resultA) .. 在您的循环中:chats.forEach(function(resultA)..

It seems like you haven't declare 'chats' variable. 似乎您尚未声明“聊天”变量。

Also, as Parse official recommended , please try following: 另外,按照Parse官方的建议,请尝试以下操作:

var _ = require('underscore.js');

var promise = Parse.Promise.as();
_.each(resultsA, function(resultA) {

    promise = promise.then(function() {..}.then..then..
}
return promise;

Hope this can help you. 希望这可以帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM