2013-08-13 3 views
0

저는 응용 프로그램을 개발 중이며 한 번에 많은 항목을 추가해야합니다.
node.js로 어떻게 할 수 있습니까? parse.com db에 객체 배열을 추가하는 방법은 무엇입니까?

This

는 parse.com에 대한 NPM 모듈입니다하지만 단일 개체마다 시간을 삽입하지 않으

insertAll("Foo", [objs...], ...) 

같은 방법이 없습니다.

+0

구문 분석이 그들의 서비스에 다중 삽입을 지원하기 위해 표시되지 않습니다를 작성해야합니다. 그래서 콜렉션을 반복하고 각각에 대해'.insert() '를 호출해야 할 것이다. –

답변

1

응용 프로그램과 parse.com 사이를 연결하는 편리한 기능을 작성하십시오. https://parse.com/docs/rest#objects-creating : 당신은 한 번 반복 코드 (또는 디버그 광산)

var async = require('async'); 
var parseApp = require('node-parse-api').Parse; 
var APP_ID = ""; 
var MASTER_KEY = ""; 
var parseApp = new Parse(APP_ID, MASTER_KEY); 

function insertAll(class, objs, callback){ 
    // create an iterator function(obj,done) that will insert the object 
    // with an appropriate group and call done() upon completion. 

    var insertOne = 
    (function(class){ 
     return function(obj, done){ 
     parseApp.insert(class, obj, function (err, response) { 
      if(err){ return done(err); } 
      // maybe do other stuff here before calling done? 
      var res = JSON.parse(response); 
      if(!res.objectId){ return done('No object id') }; 
      done(null, res.objectId); 
     }); 
     }; 
    })(class); 

    // async.map calls insertOne with each obj in objs. the callback is executed 
    // once every iterator function has called back `done(null,data)` or any one 
    // has called back `done(err)`. use async.mapLimit if throttling is needed 

    async.map(objs, insertOne, function(err, mapOutput){ 
    // complete 
    if(err){ return callback(err) }; 
    // no errors 
    var objectIds = mapOutput; 
    callback(null, objectIds); 
    }); 
}; 

// Once you've written this and made the function accessible to your other code, 
// you only need this outer interface. 

insertAll('Foo', [{a:'b'}, {a:'d'}], function(err, ids){ 
    if(err){ 
    console.log('Error inserting all the Foos'); 
    console.log(err); 
    } else { 
    console.log('Success!); 
    }; 
}); 
+0

문제는 수백만 개의 항목이 있고 요청에 대한 구문 분석에 제한이 있습니다 (1 백만) – shift66

+0

[배치 api] (https://parse.com/docs/rest#objects-batch)를 사용하거나 확장 성이 뛰어난 저장소를 찾으십시오 시스템 – Plato

관련 문제