使用Parse的Javascript - 使用异步循环保存

时间:2015-03-17 11:23:20

标签: javascript loops pointers parse-platform

我保存了三个结果,但它们都是一样的。我遇到了大坝Javascript异步循环问题。我怎么摆脱这个?

我的代码:

 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();

2 个答案:

答案 0 :(得分:1)

问题是matched.save();是一个异步操作。在完成所有保存之前,您的功能很可能正在返回。克服这个问题最好的办法就是使用承诺。请在此处阅读:https://parse.com/docs/js_guide#promises

然后查看Parse JavaScript SDK and Promise ChainingMultiple Queries Parse Javascript

根据您的问题调整上述解决方案将如下所示:

 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();等待所有传递给它的承诺得到解决。

修改

我不确定matched变量的来源,但这里是更新的代码

 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();

答案 1 :(得分:1)

错误在于我提升了匹配的班级。匹配需要在循环内定义。代码应如下所示:

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();