2

I have been trying to run my Parse Cloud Code for some time and can not seem to get around this problem:

I have an array of Parse objectId's called IDArray. I then am sending the array as a parameter of a PFCloud call. Once the array has been sent to the Cloud Code, I can not seem to successfully create a for loop that goes through and updates a number value stored as "points" on Parse for each objectId.

In a nutshell, this is all I am trying to accomplish:

  • I just need to be able to have the for loop go through each objectId and perform an action for each ID.

I have been trying to get this to work for some time but have had no luck. Here is the code that I have been trying to manipulate - hopefully it will give someone a starting point to answer my question.

Parse.Cloud.define('updateAllUsers', function(request, response) {
    var UserData = Parse.Object.extend('UserData');
    var query = new Parse.Query(UserData);
    var list = request.params.listID;
    var currentuser = request.params.user;

                   
    for (var i = 0; i < list.length; i++) {
                   
        var userdata = list[i];        
        query.get(userdata, {
                                       
            success: function(UserData) {
                               
                response.success('Should add up');
                UserData.addUnique('Done', +1);
                UserData.save();
            },
            error: function() {
                response.error('something went wrong' );
            }
        });
    }
});

If someone could please help me with this I would be very grateful. Thank you

7
  • What is the value of request.params.listID at runtime? And do you go down the error path? Commented Aug 13, 2015 at 23:49
  • the value of "listID" is an array that contains Parse objectId's. And this is the error that I am receiving in my NSLog: [Error]: Error: Can't call success/error multiple times Commented Aug 14, 2015 at 0:00
  • @KevinBoucher not really sure where to go from here. I have been trying to get this to work using promises, but I cant seem to get it right Commented Aug 14, 2015 at 0:22
  • I think it's because you are using the same Parse.Query over and over again. Move the query assignment into the loop. Commented Aug 14, 2015 at 1:21
  • how would you put the query assignment into the loop? Commented Aug 14, 2015 at 1:23

1 Answer 1

1

I think the problem is you are sending response multiple times, you should wait for all the promises to finish and then send a response:

Parse.Cloud.define('updateAllUsers', function(request, response) {
    var UserData = Parse.Object.extend('UserData');
    var query = new Parse.Query(UserData);
    var list = request.params.listID;
    var currentuser = request.params.user;

    function checkUserData(userdata){   // returns parse promise for a particular userdata
        return query.get(userdata).then(function(){
            UserData.addUnique('Done', +1);
            UserData.save();            
        });
    }

    Parse.Promise.when(list.map(checkUserData)) // mapping all the elements in the list to resp promises
        .then(function(){   // on success
            response.success('Should add up');
        }).catch(function(e){    // on failure
             response.error('something went wrong' );
        });
});

Edit: if for some reason map is not available( in case of older browsers or list not being an normal javascript array), you can do something like:

Parse.Cloud.define('updateAllUsers', function(request, response) {
    var UserData = Parse.Object.extend('UserData');
    var query = new Parse.Query(UserData);
    var list = request.params.listID;
    var currentuser = request.params.user;
    var promises = [];
    function checkUserData(userdata){   // returns parse promise for a particular userdata
        return query.get(userdata).then(function(){
            UserData.addUnique('Done', +1);
            UserData.save();            
        });
    }

    for(var i=0;i<list.length;i++){
        promises.push(checkUserData(list[i]));
    }


    Parse.Promise.when(promises) // once all the promises are resolved...
        .then(function(){   // on success
            response.success('Should add up');
        }).catch(function(e){    // on failure
             response.error('something went wrong' );
        });
});
Sign up to request clarification or add additional context in comments.

9 Comments

thanks for your help so far. I am new to this so bare with me lol. So this will take the "n" items (which are all objectId's from "listID") and create promises for those n items? Im confused on how this will loop through each of the items separately. However I did try using this but received this: [Error]: function not found (Code: 141, Version: 1.7.5)
yes i created list as an NSArray. currently it has 4 test objectId values but it will need to be some number later on when this works
I am pulling the list array from my xcode project - not sure if that is clear or if it changes things
@WillVonUllrich you can try my second option then, one without map, also spraying console.log all over the place might give a clearer idea where the error is coming from
alright. last question: the checkUserData function - does that work in conjunction with the Parse.Promise... after the for loop?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.