Query Inside Parse Cloud For Loop
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 a
Solution 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);
varlist = request.params.listID;
var currentuser = request.params.user;
functioncheckUserData(userdata){ // returns parse promise for a particular userdatareturn 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);
varlist = request.params.listID;
var currentuser = request.params.user;
var promises = [];
functioncheckUserData(userdata){ // returns parse promise for a particular userdatareturn 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' );
});
});
Post a Comment for "Query Inside Parse Cloud For Loop"