jeudi 26 mars 2015

Saving within nested loops with promises


I want to save an object called singleInfo in a column for qualifying parseObjects as shown below. The code below chooses the right objects to save to but it only saves data intended for the last object to all of the objects. I'm pretty sure it is a problem with how I'm doing the loops. I thought the promises would resolve this. I used a similar promise pattern in the past that provided good results but it's not working this time. I think it has something to do with the nested loops as before I only used this patter on a single loop.



var func = function(){
var promise = Parse.Promise.as();
var array = [];
var pairs = [parseObject, parseObject, parseObject];
var a = pairs.length;
while(a--){
var pair = pairs[a];
var singles = [obj, obj, obj, obj, obj];
var b = singles.length;
while(b--){
var single = singles[b];
if(single.value === pair.get('value')){
var singleInfo = {news: single.news, time: pair.get('time')}
query.get(pair.id, {
success: function (goal){
goal.set('newColumn', singleInfo);
array.push(goal.save())
}
})
}
}
promise.then(function(){
return Parse.Promise.when(array);
}




Aucun commentaire:

Enregistrer un commentaire