简体   繁体   English

如何使用 nodeJS 在 Aws DynamoDB 中进行更新查询

[英]how make update query in Aws DynamoDB using nodeJS

I need to update one item of DynamoDB, here is my Code.我需要更新 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));
            }
        })

I am facing issues while running this code here my error.我在此处运行此代码时遇到问题我的错误。

Unable to update item.无法更新项目。 Error JSON: {错误 JSON:{
"message": "Missing required key 'Key' in params", "code": "MissingRequiredParameter", "message": "在参数中缺少必需的键 'Key'", "code": "MissingRequiredParameter",
"time": "2019-07-20T07:47:06.490Z" } “时间”:“2019-07-20T07:47:06.490Z” }

But you can see I have Key object in params but still struggling for this.但是您可以看到我在 params 中有 Key 对象,但仍在为此苦苦挣扎。

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

The problem is in Key, You need to use primary key to find which row will you update in Table Like SQL such as Primary Key.问题出在Key上,你需要使用主键来查找你将在Table Like SQL中更新哪一行,比如Primary Key。 IN DynamoDB Primary Key = PK+SK example if your: PK = 'cardid' AND SK = 'name'在 DynamoDB 主键 = PK+SK 示例中,如果您:PK = 'cardid' AND SK = 'name'

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM