0

I'm saving three results but they are all the same. I'm running into the dam Javascript async loop problem. How do I get out of this?

my code:

 var teamMatch = function(){
     var Pairingteam = [];
     var promise = Parse.Promise.as();
     var x = matchedPairingIds.length;
     while (x--) {
         var match = matchedPairingIds[x];
         var matching = function () {
             matched.set("team", {
               __type: "Pointer",
               className: "_User",
               objectId: "yCDDEWoiwM"
               });
             matched.set("Pairing", {
              __type: "Pointer",
              className: "Pairing",
              objectId: match
              });
             Pairingteam.push(matched.save());
            }
          }
          promise.then(function(){
            return Parse.Promise.when(Pairingteam);
          });
      }
     teamMatch();
rashadb
  • 2,515
  • 4
  • 32
  • 57

2 Answers2

1

The issue is that matched.save(); is an asynchronous operation. Most likely your function is returning before all the saves are complete. Your best bet to overcome this is to use promises. Read up on them here: https://parse.com/docs/js_guide#promises

Then take a look at Parse JavaScript SDK and Promise Chaining and Multiple Queries Parse Javascript

Adapting the above solutions to your question would look something like this:

 var partnerMatch = function() {
 var savePromises = [];  // this will collect save promises 

// Create a trivial resolved promise as a base case.
 var promise = Parse.Promise.as();
 var x = matchArray.length // x = 3
 while (x--) {
     var matched = matchArray[x];
     var matching = function() {
         matched.set("partner", {
             __type: "Pointer",
             className: "_User",
             objectId: "yCDDEWoiwM"
         });
         matched.set("match", match);
         savePromises.push(matched.save());
     }
 }

 promise.then(function(){
   // now do the saves
   return Parse.Promise.when(savePromises);
 });
 }
 partnerMatch();

Promise.When(); waits around until all of the promises passed to it are resolved.

EDIT

I'm not sure where your matched variable is coming from, but here's the updated code

 var teamMatch = function(){
 var Pairingteam = [];
 var promise = Parse.Promise.as();
 var x = matchedPairingIds.length;
 while (x--) {
     var match = matchedPairingIds[x];
     matched.set("team", {
       __type: "Pointer",
       className: "_User",
       objectId: "yCDDEWoiwM"
       });
     matched.set("Pairing", {
      __type: "Pointer",
      className: "Pairing",
      objectId: match
      });
     Pairingteam.push(matched.save());
     }
      promise.then(function(){
        return Parse.Promise.when(Pairingteam);
      });
  }
 teamMatch();
Community
  • 1
  • 1
toddg
  • 2,863
  • 2
  • 18
  • 33
  • A little extra detail about where I put it or how to implement? Do you mean 'then'? I'm familiar with 'then' but I don't know how to implement it in this case. I'm looking up Promise.When(); – rashadb Mar 17 '15 at 12:48
  • I'm definitely getting promises and 3 of them as I'd expect from the savePromises array you proposed. Unfortunately it is still only saving one of these values. – rashadb Mar 17 '15 at 13:05
  • Updated to include base promise – toddg Mar 17 '15 at 13:20
  • The Parse SDK is completely useless. I see the other illustrations and they're somewhat instructive but I'm not connecting the dots. The Promise.when() is a nice little trick but the example you show and the ones illustrated in the examples you provide are saving 3 identical in the array and only one parse object instead of 3 unique Parse.Objects. – rashadb Mar 17 '15 at 13:21
  • Ok, it feels like I'm getting closer but now I'm getting no results in the array. I'm thinking that maybe I need to call the matching(); function? I appreciate the guidance here . . . one more step please? – rashadb Mar 17 '15 at 13:34
  • I updated the code to where I'm at now. I'm receiving no results in my array with this code. – rashadb Mar 17 '15 at 13:55
  • Thanks for chiming back. If I mentioned it, I tried it already and that still gives me 3 identical promises and which should be 3 rows in the matched class but it is only one row. To be clear it should be 3 different promises in the array and 3 different rows upon the save. – rashadb Mar 17 '15 at 14:32
  • thanks for circling back on this. matched is the new Parse.Object I want to save the pointer pairs to. In this example there are 3 pointer pairs as x = 3. Only one is saving based on the code your providing. And all of the promises in the Pairingteam array are identical. – rashadb Mar 17 '15 at 14:48
  • Are you successfully getting the proper match from your matchedPairingIds? How do you know the promises are identical? Where are you declaring `var matched = new Parse.Object`? I'm having a hard time diagnosing your problem. – toddg Mar 17 '15 at 17:21
  • I hoisted var matched = new Parse Object. I'm sure of the matched PairIds, I don't much but anything I do know comes from console.log("he he"). But you're right, I don't know if the promises are identical - I got that confused with the identical array results I would get from before. – rashadb Mar 17 '15 at 20:58
1

The mistake was that I hoisted the matched class. Matched needs to be defined within the loop. The code should look like this:

var teamMatch = function(){
     var Pairingteam = [];
     var promise = Parse.Promise.as();
     var x = matchedPairingIds.length;
     while (x--) {
         var matched = new Parse.Object("Matched"); //THIS IS THE MISSING PIECE
         var match = matchedPairingIds[x];
         var matching = function () {
             matched.set("team", {
               __type: "Pointer",
               className: "_User",
               objectId: "yCDDEWoiwM"
               });
             matched.set("Pairing", {
              __type: "Pointer",
              className: "Pairing",
              objectId: match
              });
             Pairingteam.push(matched.save());
            }
          }
          promise.then(function(){
            return Parse.Promise.when(Pairingteam);
          });
      }
     teamMatch();
rashadb
  • 2,515
  • 4
  • 32
  • 57