【问题标题】:how make update query in Aws DynamoDB using nodeJS如何使用 nodeJS 在 Aws DynamoDB 中进行更新查询
【发布时间】:2021-02-26 16:42:53
【问题描述】:

我需要更新一项 DynamoDB,这是我的代码。

const Aws = require('aws-sdk');
const endPoint= 'http://dynamodb.us-east-2.amazonaws.com';
const IAm_user_Key=process.env.IAM_USER_KEY;
const IAm_user_Secret=process.env.IAM_USER_SECRET;

Aws.config.update({ accessKeyId:IAm_user_Key,secretAccessKey:IAm_user_Secret,region:'us-east-2',endpoint:endPoint });

var docClient = new Aws.DynamoDB.DocumentClient();
var table = "cards";

var params={
        TableName:table,
        key:{ "cardid":clientData.data.cardid},
        AttributeValue:{"data":clientData.data}
  }

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

我在这里运行此代码时遇到问题我的错误。

无法更新项目。错误 JSON:{
"message": "参数中缺少必需的键 'Key'", "code": "MissingRequiredParameter",
“时间”:“2019-07-20T07:47:06.490Z” }

但是您可以看到我在 params 中有 Key 对象,但仍在为此苦苦挣扎。

【问题讨论】:

  • 如果你想put,你应该使用docClient.update而不是docClient.put。请参阅docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/… 了解更多信息。看起来您正在将更新参数传递给 put 方法。
  • @Armin 对不起我的错误,我编辑我的问题我需要 docClient.update 而不是 put

标签: node.js amazon-web-services amazon-dynamodb


【解决方案1】:

我阅读文档太多次,但出现了一个愚蠢的错误,我发现它只是拼写错误,params 中的“key”应该是“Key”K 仅大写,这是有效的。

【讨论】:

  • 我犯了同样的错误:D
  • 我有同样的问题,但我的钥匙是大写的:(
【解决方案2】:

问题出在Key上, 您需要使用主键来查找您将在 Table Like SQL 中更新哪一行,例如 Primary Key。在 DynamoDB 中 主键 = PK+SK 例如,如果您: PK = 'cardid' AND SK = '姓名'

var params={
        TableName:table,
        Key:{ "cardid":clientData.data.cardid, 'name': req.body.name},
        AttributeValue:{"data":clientData.data}
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    • 2019-09-26
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    相关资源
    最近更新 更多