title: Cloud code on parse.com skipping a save
body: <p>I'm trying to set up a game that allows playing with random players. The code below is supposed to create a GameMessage object for both paired players. To relate both objects as part of the same game, I've decided to save the objectId of of the game made for "firstplayer" in the field "otherside" for "secondplayer" and vice-versa. For some reason (perhaps the first save of firstplayer and secondplayer isn't done before the code attempts to retrieve the objectIds, meaning there are no objectIds to get?). </p>
  
  <p>Short version: Why are the "otherside" values not saving?</p>
  
  <pre><code>Parse.Cloud.define("findpartner", function(request, response) {
  
  var User = Parse.Object.extend("_User");
  var user = new User();
  
  var currentuser = Parse.User.current();
  currentuser.set("searching", 0);
  
  
  var query = new Parse.Query(User);
  query.equalTo("searching", 1);
  query.limit(50); //limit to at most 50 users
  query.find({
      success: function(objects) {
          var amount = objects.length;
          var indexNum = Math.floor((Math.random() * amount));
           var newpartner = objects[indexNum];
                     if (amount &gt; 0 &amp;&amp; newpartner.id !=currentuser.id) {
  
  
  
              newpartner.set("searching", 0);
  
              var Firstplayer = Parse.Object.extend("GameMessages");
              var firstplayer = new Firstplayer();
  
              var Secondplayer = Parse.Object.extend("GameMessages");
              var secondplayer = new Secondplayer();
  
              firstplayer.set("sender", currentuser.id);
              firstplayer.set("receiver", newpartner.id);
              firstplayer.set("sent",0);
              firstplayer.set("received",0);
              firstplayer.set("receiverName", newpartner.getUsername());
  
              secondplayer.set("sender", newpartner.id);
              secondplayer.set("receiver", currentuser.id);
              secondplayer.set("sent",0);
              secondplayer.set("received",0);
              secondplayer.set("receiverName", currentuser.getUsername());
  
              firstplayer.save().then(function(secondplayer){               &lt;&lt;&lt;
                  return secondplayer.save();                               &lt;&lt;&lt;
  
                  }).then(function(firstplayer_update) {                    &lt;&lt;&lt;
                  return firstplayer.save({ otherside: secondplayer.id});   &lt;&lt;&lt;
  
              }).then(function(secondplayer_update){                        &lt;&lt;&lt;
                  return secondplayer.save({ otherside: firstplayer.id});   &lt;&lt;&lt;
  
              });
  
  
              newpartner.save(null, {useMasterKey: true});
  
          }
                      else {
                          currentuser.set("searching", 1);
                      }
          currentuser.save();
          response.success(amount);
        },
      error: function(error) {
          alert("Error: " + error.code = " " + error.message);
        }
     });    
  
  });
  </code></pre>
  
  <p>I added arrows to show where the "otherside" is. They're not in the actual code. I do not doubt the code has mistakes though, I do not know javascript. I wrote it solely by studying the parse.com documentation. </p>