javascript - AWS Node.js JSON 输入错误

标签 javascript json node.js amazon-web-services amazon-dynamodb

我尝试使用 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);
       }
    });
});

最佳答案

当您循环调用 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)
    ));
}

关于javascript - AWS Node.js JSON 输入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38082696/

相关文章:

javascript - 如何获取对象元素的序号?

javascript - 在 ASP.NET 代码中通过 javascript 发出警报后停止回发

javascript - ReactJs:从数组创建组件会导致旧 Prop

javascript - 为什么我的脚本快速登录到控制台然后清除?

json - 如何解码mongodb数据以构造

c# - 从 JSON 获取属性名称

json - 使用 Lift-json 使用 Map[String,Any] 属性反序列化案例类

javascript - 如何使用 Vue 构建动态 CSS 网格?

javascript - JSON.stringify() 灾难性的

html - 如何在 AWS Lambda 中将 HTML 转换为 PDF 或图像