簡體   English   中英

使用Node.js異步保存到dynamodb

[英]Saving to dynamodb asynchronously using nodejs

因此,我具有以下功能,但是當我調用保存組件時,實際上只有最后一個條目保存到數據庫中。

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

我將如何獲取保存以異步方式調用,以便所有條目都保存在數據庫中?

似乎與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");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM