我有以下代码我试图使用Node.js上传到DynamoDB本地主机。
是否有可能的解决方法。对于以下错误?
Unable to add event undefined . Error JSON: {
"message": "One of the required keys was not given a value",
"code": "ValidationException",
"time": "2016-06-28T04:02:26.250Z",
"requestId": "970984e4-3546-41f0-95f9-6f1b7167c510",
"statusCode": 400,
"retryable": false,
"retryDelay": 0
}
这是代码。我希望Item: {}
接受可能存在的任何值,并将它们添加到表中。
var AWS = require("aws-sdk");
var fs = require('fs');
AWS.config.update({
region: "us-west-2",
endpoint: "http://localhost:8000"
});
var docClient = new AWS.DynamoDB.DocumentClient();
console.log("Importing movies into DynamoDB. Please wait.");
var allMovies = JSON.parse(fs.readFileSync('moviedata.json', 'utf8'));
allMovies.forEach(function(movie) {
var params = {
TableName: "Movies",
Item: {
"year": movie.year,
"title": movie.title,
"info": movie.info,
"twitter": movie.twitter
}
};
docClient.put(params, function(err, data) {
if (err) {
console.error("Unable to add movie", movie.title, ". Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("PutItem succeeded:", movie.title);
}
});
});
答案 0 :(得分:0)
当您遍历一个promise调用时,您需要一种保护措施,在开始下一个promise之前,当前的promise会解析。
var AWS = require("aws-sdk");
var fs = require('fs');
const tableName = 'Movies';
AWS.config.update({
region: "local",
endpoint: "http://localhost:8000"
});
var docClient = new AWS.DynamoDB.DocumentClient();
console.log("Importing movies into DynamoDB. Please wait.");
var allMovies = JSON.parse(fs.readFileSync('moviedata.json', 'utf8'));
for (let i = 0, p = Promise.resolve(); i < allMovies.length; i++) {
p = p.then(_ => new Promise(resolve =>
setTimeout(function () {
var params = {
TableName: tableName,
Item: {
"year": allMovies[i].year,
"title": allMovies[i].title,
"info": allMovies[i].info
}
};
docClient.put(params, function(err, data) {
if (err) {
console.error("Unable to add movie", allMovies[i].title, ". Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("PutItem succeeded:", allMovies[i].title);
}
});
resolve();
}, 10)
));
}