解析服务器将数据串联保存

时间:2016-12-14 18:06:08

标签: javascript json parse-platform

所以我们有JSON格式的大数据。 我们希望将其保存到Parse应用中的class (table)。 我写了一个JS脚本,它可以读取文件并浏览JSON数据。 但是什么时候保存它会搞砸。它的循环在第一个永远。我知道有一些叫做promise的东西,但我不明白如何使用它?谁能帮忙。我的代码如下。

function processJson(result) {
object = JSON.parse(result);
verbose.textContent = "Read " + object.results.length + " objects";
var count = object.results.length;
var countAc = 0;
logger("To save: " + count);
i = 0;
while (i < count) {
    if (object.results[i].areaType == 'ac') {
        save(i).then(function (object) {
            i = i + 1;
            logger("Success: " + object.id);
        });
    } else {
        logger("ac not found");
        i = i + 1;
    }
}
}

function save(i) {
    logger("ac found");
    var constituency = new Constituency();
    constituency.set("points", object.results[i].points);
    constituency.set("areaType", object.results[i].areaType);
    constituency.set("name", object.results[i].name);
    constituency.set("state", object.results[i].state);
    constituency.set("index", object.results[i].index);
    constituency.set("pc", object.results[i].pc);
    constituency.set("center", object.results[i].center);
    constituency.set("oldObjectId", object.results[i].objectId);

return constituency.save();

/*constituency.save().then(function(obj) {
    // the object was saved successfully.
    i = i + 1;
    logger("Success: " + obj.id);
}, function(error) {
    // the save failed.
    logger(error.message);
    i = i + 1;
});*/
}

1 个答案:

答案 0 :(得分:0)

我会做那样的事情:

function processJson(result) {
  
  var object = JSON.parse(result);
  
  for (var i = 0; i < object.results.legnth; i++){
     var parseObject = createParseObjectFromJSONObject(object.results[i]);
     parseObject.save(null).then(function(object){
       console.log("object saved: " + object.id);
     },function(error){
        console.log("error: " + error);
     });
  }
  
}

function createParseObjectFromJSONObject(jsonObject){
   var constituency = new Constituency();
   constituency.set("points", jsonObject.points);
   constituency.set("areaType", jsonObject.areaType);
   constituency.set("name", jsonObject.name);
   constituency.set("state", jsonObject.state);
   constituency.set("index", jsonObject.index);
   constituency.set("pc", jsonObject.pc);
   constituency.set("center", jsonObject.center);
   constituency.set("oldObjectId", jsonObject.objectId); 
   return constituency;
}

你可以做得更好......

您可以先将所有解析对象推送到数组中,然后调用 saveAll 将所有解析对象保存在一个请求中。该解决方案适用于&lt; 1000条记录..如果你有超过1000条,那么你可以做分页(第1000条和saveAll,其他1000条和saveAll ....)

在此版本中,您的代码将如下所示:

    function processJson(result) {
      
      var object = JSON.parse(result);
      var allObjects = [];
      
      for (var i = 0; i < object.results.legnth; i++){
         var parseObject = createParseObjectFromJSONObject(object.results[i]);
         allObjects.push(parseObject);
      }
      
      // outside the loop we are ready to save all the objects in 
      // allObjects array in one service call!
      if (allObjects.length > 0){
        
          Parse.Object.saveAll(allObjects).then(function(){
            console.log("all objects were saved!");
            // all object ids are now available under the allObjects array..
          },function(error){
              console.log("error: " + error);
          });   
      }
  
    }

    function createParseObjectFromJSONObject(jsonObject){
       var constituency = new Constituency();
       constituency.set("points", jsonObject.points);
       constituency.set("areaType", jsonObject.areaType);
       constituency.set("name", jsonObject.name);
       constituency.set("state", jsonObject.state);
       constituency.set("index", jsonObject.index);
       constituency.set("pc", jsonObject.pc);
       constituency.set("center", jsonObject.center);
       constituency.set("oldObjectId", jsonObject.objectId); 
       return constituency;
    }

祝你好运:)