0

So I have the following function but when I call save component only the last entry is actually saving to the database.

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

How would I get save to be called asynchronously so all entries save in the database?

1 Answer 1

1

Looks like an issue with javascript closures. Try.

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");
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

this did not fix the problem.
Could you provide more details in the question. I don't see any DynamoDB API calls according to: AWSJavaScriptSDK

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.