简体   繁体   English

BatchWriteItemCommand 与 AWS.DynamoDB class 在 Nodejs 中使用 AWS SDK V3

[英]BatchWriteItemCommand with AWS.DynamoDB class using AWS SDK V3 in Nodejs

I have been trying for hours to perform a DynamoDB DeleteRequest using BatchWriteItemCommand but I keep getting the following error:我一直在尝试使用 BatchWriteItemCommand 执行 DynamoDB DeleteRequest 数小时,但我不断收到以下错误:

Error ValidationException: 1 validation error detected: Value null at 'requestItems.td_notes_sdk.member.1.member.deleteRequest.key' failed to satisfy constraint: Member must not be null

This is what my table looks like:这是我的桌子的样子:

Partition key: user_id (string)分区键:user_id(字符串)

Sort key: timestamp (number)排序键:时间戳(数字)

DynamoDB Screenshot DynamoDB 截图

This is what my code looks like:这是我的代码的样子:

// Import required AWS SDK clients and commands for Node.js
import {
  DynamoDBClient,
  BatchWriteItemCommand,
} from "@aws-sdk/client-dynamodb";

// Set the parameters
export const params = {
  RequestItems: {
    "td_notes_sdk": [
      {
        DeleteRequest: {
          Item: {
              Key: {
                  user_id: { S : "bb" },
                  timestamp: { N : 2 },
                },
          },
        },
      },
    ],
  },
};

export const run = async () => {
  const ddbClient = new DynamoDBClient({ region: "us-east-2" });
  try {
    const data = await ddbClient.send(new BatchWriteItemCommand(params));
    console.log("Success, items inserted", data);
    return data;
  } catch (err) {
    console.log("Error", err);
  }
};
run();

Here are some resources that I've been trying to follow along with: Resource 1: Writing items in Batch Example Resource 2: AWS Javascript SDK v3 Documentation以下是一些我一直在尝试遵循的资源:资源 1: 在批处理示例中编写项目资源 2: AWS Javascript SDK v3 文档

Update: BatchWrite PutRequest work with the code below, so I know that the structure of my keys/attributes is closer to being correct.更新:BatchWrite PutRequest 使用下面的代码,所以我知道我的键/属性的结构更接近正确。 Still does not work for DeleteRequest.仍然不适用于 DeleteRequest。

export const params = {
  RequestItems: {
    "td_notes_sdk": [
      {
        PutRequest: {
          Item: {
            user_id: { "S": "bb" },
            timestamp: { "N": "5" },
          },
        },
      },
    ],
  },
};

You don't supply an Item when deleting an item.删除项目时不提供项目。 You supply a Key.您提供一个密钥。

Here is a working example:这是一个工作示例:

const params_delete = {
  RequestItems: {
    "td_notes_sdk": [
      {
        DeleteRequest: {
          Key: {
            user_id: { S: "bb" },
            timestamp: { N: "2" },
          },
        },
      },
    ],
  },
};

const delete_batch = async () => {
  const ddbClient = new DynamoDBClient({ region: "us-east-2" });
  try {
    const data = await ddbClient.send(new BatchWriteItemCommand(params_delete));
    console.log("Success, item deleted");
    return data;
  } catch (err) {
    console.log("Error", err);
  }
};

delete_batch();

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

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