使用nodejs异步保存到dynamodb
Saving to dynamodb asynchronously using nodejs
所以我有以下函数,但当我调用save组件时,只有最后一个条目实际保存到数据库。
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");
}
}
相关文章:
- 如何将getJson的响应保存在全局变量中
- 按下按钮时保存cookie
- 如何使用 Angular JS 将数据保存在数据库中
- 子字符串/正则表达式以获取字符串中保存的 SRC 值
- HTML5在画布中加载较小的图像并保存实际大小的图像
- 是否可以在浏览器中使用纯JavaScript保存音频流
- 如何将多个画布保存为一个图像
- PHP:显示sqlite数据库中的html格式数据,使用tinymce保存
- 如何将乳胶配方奶粉图像保存到Asp.net中的文件夹中
- 使用DynamoDB查询返回
- 保存数组javascript
- 保存串行端口列表与流星
- 将JavaScript变量保存到Rails模型
- Java脚本将URL转换为已保存的URL时出错
- 如何自动保存动态生成的HTML SVG元素
- Javascript/jQuery压缩一个图像正确的评论帖子,并使用Ajax Laravel 5.2保存到控制器中
- 将JQuery Handontable保存到服务器上的excel文件中
- 通过NodeJS将对象保存到DynamoDB实例
- 使用nodejs异步保存到dynamodb
- Dynamose/DynamoDB更新将空数组保存为null