2016-10-14 3 views
0

그래서 다음과 같은 기능을 갖지만 저장 컴포넌트를 호출하면 마지막 항목 만 실제로 데이터베이스에 저장됩니다.nodejs를 사용하여 dynamodb에 비동기 적으로 저장

var shortid = require('shortid'), 
dynamodb = new AWS.DynamoDB(); 


    setupComponents: function(args) { 
        console.log(args) 
         if (args["componentName"] && args["apiKey"] !== null){ 
          var apiKey = args["apiKey"] 
          for (i = 0; i < args["componentName"].length; i++) { 
          console.log(args["componentName"][i]); 
          var componentName = args["componentName"][i]; 
          saveComponent(componentName, apiKey); 
          } 
         } else { 
          console.log("NULL FOUND"); 
         } 
        }, 

function saveComponent(args, apiKey) { 
    var o = new SoftwareComponent({ 
     _id: shortid.generate, 
     componentName: args, 
     versionName: "default", 
     stepName: "default", 
     timeInMS: "default", 
     stepResult: "default", 
     notes: "default", 
     apiKey: apiKey 
    }); 
    console.log(o) 
    o.save(); 
} 

모든 항목이 데이터베이스에 저장되도록 저장을 비동기 적으로 호출하려면 어떻게해야합니까?

답변

1

javascript 클로저의 문제처럼 보입니다. 시험.

setupComponents: function (args) { 
    console.log(args); 
    if (args["componentName"] && args["apiKey"] !== null) { 
     var apiKey = args["apiKey"]; 
     var arr = args["componentName"]; 
     arr.forEach(function(item, index){ 
      console.log(item); 
      saveComponent(item, apiKey); 
     }); 
    } else { 
     console.log("NULL FOUND"); 
    } 
} 
+0

이렇게해도 문제가 해결되지 않았습니다. – user3236101

+0

질문에 더 자세히 설명해 주시겠습니까? DynamoDB API 호출은 다음과 같이 표시되지 않습니다. [AWSJavaScriptSDK] (http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#updateItem-property) –

관련 문제