javascript - Parse promises and loop of query -


i cannot figure out how ca apply promise paradigm code. code doesn't work out how should. last 2 queries not performed... first (mainquery) limited <=5 items should fast enough. pipeline should be: query1.find()->for elements found->if element of type 1 -> query2.count()->if count == 0-> save new object

could please me fix it? thank in advance, michele

mainquery.find().then(         function(items){             (var = 0; < items.length; i++) {                                if(items[i].get("type") == 1){                              var query = new parse.query("invites");                     //query.equalto("userid","aaaaaaaa")                     query.count().then(function(count){                           console.log("why don't see in logs...??");                           if (count == 0){                             var invite = new parse.object("invites");                             //invite.set("userid", "aaaaaaa");                             invite.save();                           }                         }, function(error){                         response.error("msgs lookup failed");                     });                  }             }             response.success(items);         },         function(error) {response.error("msgs lookup failed");     });     

i think problem you're calling response.success before other queries have been executed. code should more like:

mainquery.find().then(     function(items){         var promises = [];         (var = 0; < items.length; i++) {             if(items[i].get("type") == 1){                 var query = new parse.query("invites");                 //query.equalto("userid","aaaaaaaa")                 // first promise needs resolved                 var countpromise = query.count().then(function(count){                     console.log("why don't see in logs...??");                     if (count == 0){                         var invite = new parse.object("invites");                         //invite.set("userid", "aaaaaaa");                         // instead of resolved object, return promise resolved                          // before entering success part below                         return invite.save();                     } else {                         // if there nothing do, return promise resolved dummy value                         return promise.as('nothing do');                     }                 });                 promises.push(countpromise);             }         }         return parse.promise.when(promises).then(function (results) {             // in here when promises aboth has been resolved             response.success(items);         });     },     function(error) {         // if of requests fail, method called         response.error("msgs lookup failed");     }); 

you'll find additional information on how chain promises on official documentation: https://parse.com/docs/js/guide#promises-promises-in-parallel


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -