簡體   English   中英

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

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

我一直在嘗試使用 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

這是我的桌子的樣子:

分區鍵:user_id(字符串)

排序鍵:時間戳(數字)

DynamoDB 截圖

這是我的代碼的樣子:

// 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();

以下是一些我一直在嘗試遵循的資源:資源 1: 在批處理示例中編寫項目資源 2: AWS Javascript SDK v3 文檔

更新:BatchWrite PutRequest 使用下面的代碼,所以我知道我的鍵/屬性的結構更接近正確。 仍然不適用於 DeleteRequest。

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

刪除項目時不提供項目。 您提供一個密鑰。

這是一個工作示例:

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