Reputation: 5
Parse.Cloud.define("getPicks", function(request,response){
var picks = new Parse.Query("Picks");
var num=0;
picks.find({
success: function(results)
{
if ( results.length == 0 )
{
// No picks in databade
response.success("No picks are live");
}
else
{
for(var i=0;i<results.length;i++){
result=results[i];
result.set("winningTeam",3);
result.save();
num++;
}
response.success("Found "+results.length+" pick objects!"+" num is "+num);
}
},
error: function(error) {
response.error("Query failed. Error = " + error.message);
}
});
});
Im trying to update all of these pick objects to set their winning team to "3", I want to do something with every object in the database (i'll do that after i have this straightened out) then set the "winningTeam" field to 3. It works OK but for some reason if I have say.. 20 objects in the database, only 9 of them get updated when I run this code, if I have 50 objects, 9 get updated, if I have 9 or less they all get updated, kinda confused as to why. I'm new to parse cloud and to javascript so I'm probably just doing something stupid or not saving them right idk, but it finds all of the objects, but only updates some of them.
Upvotes: 0
Views: 51
Reputation: 3215
Try waiting for all the save()
calls to return before calling response.success()
:
// find your picks
picks.find()
// when it finishes
.then(function(results) {
// build an array with a promise for each save call
var savePromises = results.map(function(result) {
return result.save({winningTeam: 3});
});
// and wait for all of them to finish
return Parse.Promise.when(savePromises);
}).then(function() {
// if everything went ok
response.success();
}, function(err) {
// if something bad happened
response.error(err);
});
Upvotes: 2