2017-03-15 2 views
-2

나는 그 객체를 반복 할 수있는 객체의 객체를 가지고 있습니다. 각 객체는 함수 순차에 대해 작업 할 것입니다. 첫 번째 함수 콜백은 두 번째 함수 작동합니다. 두 번째 함수 콜백은 다음 객체를 반복합니다. node.jsasync.each는 콜백 함수에서 작동하지 않습니다.

작업

위해 appliction 우리는 async.each 사용할 수 있지만 제대로 작동하지 않습니다.

async.each(result,function(item,callback){ 
    keys1 ='id' 
    keys2 ='resul' 
    var key1value = item[keys1] 
    var key2value = item[keys2] 
    var query = '{"query":{"query_string":{"query":"' + keys1 + ':' + key1value + ' AND ' + keys2 + ':' + key2value + '"}},"sort":{"_id":{"order":"asc"}}}' 
    client.search({ index: config.indexname, type: config.basetype, body: query }).then(function (data) { 
     if(err){ 
      callback(err); 
      return; 
      }else{ 
      if (data.hits.hits.length == 0) { 
       client.index({ index: config.indexname, type:config.basetype, body: data.hits.hits }, function (err, results) { 
        console.log("Inserted data"); 
        callback("inserted"); 
       }); 
      } else { 
       console.log("--- Response ---"); 
       var rows={} 
       rows=result[i]; 
       rows._catalogId = data.hits.hits[0]._source._catalogId; 
       rows._connectionId =data.hits.hits[0]._source._connectionId; 
       rows._dataImportId =data.hits.hits[0]._source._dataImportId; 
       var param = { index: config.indexname, type: config.basetype, id: data.hits.hits[0]._id, body: { doc:rows, doc_as_upsert: true } }; 
       client.update(param, function (err, results) { 
        console.log("updated data"); 
        callback("updated"); 
        }); 
       } 
      } 

    }); 
}); 
+0

일부 코드 –

+0

Manan, plz help me. – user2728653

+0

이 오류 인 것 같습니다. var key1value = result [0] [ 'id'] var key2value = 결과 [0] [ 'resul']'var ​​key1value = item [ 'id'] var key2value = item [ 'resul'] ' –

답변

0

당신이 async API for each를 읽는다면, 당신은이 기능을 병렬로 각 항목에 iteratee을 적용하기 때문에, iteratee 기능을 순서대로 완료 것이라는 보장이 없다는 것을,

주를 말한다 볼 수 있습니다 .

따라서 개체가 순차적으로 처리되지 않습니다.

관련 문제