node.js - 从 Lambda 函数更新 dynamoDB 失败

标签 node.js aws-lambda amazon-dynamodb alexa-skills-kit

我在更新 DynamoDB 条目时遇到问题。

数据库看起来像这样

{
   "userId":"amzn1.ask.account.XYZ",
   "mapAttr":{
      "name":"John",
      "colors":[
         "yellow",
         "white"
      ]
   }
}

我想使用以下代码更新颜色列表:

var docClient = new AWS.DynamoDB.DocumentClient({ apiVersion: "2012-08-10" });
var params = {
  TableName: "myTable",

  Key: {
    userId: userId
  },
  UpdateExpression: "SET #mapAttr = list_append(#mapAttr, :entry)",

  ExpressionAttributeNames: { "#mapAttr": "mapAttr.colors" },

  ExpressionAttributeValues: {
    ":entry": ["blue"]
  },

  ReturnValues: "UPDATED_NEW"
};

docClient.update(params, function(err, data) {
  if (err) {
    console.error(
      "Unable to update item. Error JSON:",
      JSON.stringify(err, null, 2)
    );
  } else {
    console.log("UpdateItem succeeded:", JSON.stringify(data, null, 2));
  }
}); 

我收到以下消息:

Unable to update item. Error JSON: { "message": "The provided key element does not match the schema", "code": "ValidationException",

有什么想法为什么......?

最佳答案

AWS documentation ,您需要拆分 ExpressionAttributeNames

UpdateExpression: "SET #mapAttr.#colors = list_append(#mapAttr.#colors, :entry)",
ExpressionAttributeNames: { 
    "#mapAttr": "mapAttr", 
    "#colors": "colors"
}

关于node.js - 从 Lambda 函数更新 dynamoDB 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50422944/

相关文章:

javascript - 错误 : Missing where attribute in the options parameter passed to findOrCreate.

amazon-web-services - AWS SQS + API 网关 + Lambda + 数据库

将Json文件实时存储到amazon S3的Python脚本

python - 为什么 sqlalchemy session 保持打开状态,即使 lambda 超时

java - DynamoDB - 使用批量操作覆盖 HashKey

javascript - 从快速请求对象中获取客户端 MAC 地址

javascript - findOneAndUpdate Mongoose w/PUT REST API

python - 如何使用给定模板生成 PDF,将 Python 或 NodeJS 中的动态数据部署在 AWS 上

amazon-web-services - 如何使用 aws iot 规则引擎更新 dynamodb 的多列

javascript - 在 dynamodb 中使用 batchWriteItem