2016-10-14 10 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(); 
} 

すべてのエントリがデータベースに保存されるように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)に従って表示されることはありません。 –

関連する問題