Parse.com success/error 未调用 我只调用了一次 succcess/error 但它不起作用

Parse.com success/error not called I'm calling succcess/error exactly once but it's not working

我试图让这个云代码功能正常工作,但我不断收到以下错误:

code: 141, message: "success/error was not called"

我肯定会传递正确的请求对象,所以这不是问题。一切都应该正确保存,但它似乎不喜欢我如何调用 success/error。

这是我的代码:

Parse.Cloud.define("redeemedCustomers", function(request, response) {
    Parse.Cloud.useMasterKey();
    console.log(request.params.consumersToUpdate);
    console.log(request.params.sref);

    _.each(request.params.consumersToUpdate, function (b) {
        var Referred = Parse.Object.extend("User");
        var referralQuery = new Parse.Query(Referred);
        referralQuery.equalTo("objectId", b);
        referralQuery.first().then(function(newbie){
            console.log(newbie);
            newbie.set("ref", request.params.sref);
            newbie.save({
                success: function () {
                    response.success("Credits were given.");
                },
                error: function (error) {
                    response.error(error, "Credits were not given.");
                }
            });
        })
    })
})

不能多次调用response.success或报错,一次只能执行一定数量的线程。使用 _.each(consumer) 时,它会在您每次执行 referralQuery.first() 请求时生成一个承诺。一旦达到限制,解析 returns 一个错误,因为在返回第一个承诺之前可能已经达到限制。此外,如果没有消费者,它永远不会到达响应。

这是我想出的解决呼叫 success/error 问题的答案。这也是 promises 的一个很好的例子:

Parse.Cloud.define("redeemedCustomers", function(request, response) {
    Parse.Cloud.useMasterKey();
    var newbiePromises = [];

    var createNewbie = function(object) {
        var promise = new Parse.Promise();
        var Referred = Parse.Object.extend("User");
        var referralQuery = new Parse.Query(Referred);
        referralQuery.equalTo("objectId", object);
        referralQuery.first().then(function(newbie) {
            newbie.set("ref", request.params.sref);
            promise.resolve(newbie.save())
        }, function(error){
            promise.reject(error);
        });

        return promise;
    }

    _.each(request.params.consumersToUpdate, function(object){
        newbiePromises.push(createNewbie(object));
    });

    Parse.Promise.when(newbiePromises).then(function(){
        response.success("It worked.");
    }, function(error) {
        response.error(error);
    });
})