-1

I'm trying to get this cloud code function working but I keep getting the following error:

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

I'm definitely passing in the correct request object so that is not an issue. Everything should save correctly but it doesn't seem to like how I'm calling success/error.

Here is my code:

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.");
                }
            });
        })
    })
})
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
rashadb
  • 2,515
  • 4
  • 32
  • 57

2 Answers2

3

Here is the answer I came up with that solved the call success/error problem. It's also a great example of 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);
    });
})
rashadb
  • 2,515
  • 4
  • 32
  • 57
-1

You can't call response.success or error multiple times and you can only execute a certain number of threads at once. When using your _.each(consumer) it will generate a promise each time you execute your referralQuery.first() request. Once the limit is reached parse returns an error because the limit was likely reached before the first promise was returned. Additionally if there were no consumers it would never reach the response.

hybrdthry911
  • 1,259
  • 7
  • 12
  • Thank you for the response but this is not helpful. Since I'm testing it, I've been sending an array with length 1 so it is neither bumping against the limit nor is it having to deal with a null/NaN/undefined issue. You can also see in the code above that success and error were called once each. – rashadb May 20 '15 at 16:07
  • Don't see a need for a downvote. My answer is valid and something you will need to address when you do pass an array of greater length. I did see one of two other issues that may cause a problem, but I guess you can figure it out. – hybrdthry911 May 20 '15 at 17:08
  • Ya, real mature. You don't know I gave you a downvote. And even if I did, how valid is it to play get back. Either my post deserves a downvote or not just as your answer is an either or - one has nothing to do with they other. If you took a moment to read my post you'd have seen that success/error was called once; I already know that so your answer was not helpful. If you want to be helpful to me or whoever else might have the same issue, maybe you should surface the extra issues you mention above. – rashadb May 20 '15 at 17:29
  • I read your post and I dont it say whether your success/error was called. Your response.error passes two variables which may cause a problem. Also your referralQuery.first().then() only handles a successful response, and does not handle an error, if no user was found with that objectID response.success/error would never be called. – hybrdthry911 May 20 '15 at 17:38
  • No worries. I posted the answer that worked for me. Hopefully it may help someone else out. @hybrdthry911 Thanks for reaching out. You are right, no down vote was necessary. I'll take a moment and do a point solid. – rashadb May 20 '15 at 22:50