从Lambda函数更新dynamoDB失败

时间:2018-05-19 07:34:26

标签: 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));
  }
}); 

我收到以下消息:

  

无法更新项目。错误JSON:{" message":"提供的密钥   元素与架构不匹配","代码":" ValidationException",

任何想法为什么......?

1 个答案:

答案 0 :(得分:0)

AWS documentation,您需要将ExpressionAttributeNames拆分。

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