我正在try 创建一个过滤器,以获取DynamoDB表中的多条记录.我在AWS Lambda中使用带有NodeJS的函数BatchGetItem作为API.下面是我的助手们的样子.

注意:我try 了多种方法来创建这些参数.我使用了单引号/双引号/不带引号;我还传递了每个项目及其类型({pk:{S:‘Request#10001’}}),但这些都不起作用.

上下文:我的表名为RequestTable,其中有一列名为"PK"(定义为字符串)和"SK"(定义为字符串).如果我在DynamoDB UI中应用完全相同的过滤器(例如:pk=="RequestTM#10001"和SK=="CLIENT"),它就可以工作.

const AWS = require('aws-sdk');
const client = new AWS.DynamoDB();

const params = {
    RequestItems: {
        "RequestTable": {
            Keys: [
                {PK: 'Request#10001', 'SK': 'Client'},
                {PK: 'Request#10002', 'SK': 'Client'}
            ]
        }
      }
    }

client.batchGetItem(params, function(err, data) {
    if (err) {
      console.log("Error", err);
    } else {
      data.Responses.RequestTable.forEach(function(element, index, array) {
        console.log(element);
      });
    }
  });

但是,我无法执行此操作,因为我总是遇到类似以下内容的错误:

Error MultipleValidationErrors: There were 6 validation errors:
* MissingRequiredParameter: Missing required key 'RequestItems' in params
* UnexpectedParameter: Unexpected key 'TableName' found in params
* UnexpectedParameter: Unexpected key 'IndexName' found in params
* UnexpectedParameter: Unexpected key 'KeyConditionExpression' found in params
* UnexpectedParameter: Unexpected key 'ExpressionAttributeNames' found in params
* UnexpectedParameter: Unexpected key 'ExpressionAttributeValues' found in params

我怎么才能解决这个问题呢?

我试着try 使用可用的选项,并试图使其看起来像以下AWS资源:Read in Batch AWS DynamoDB Example个,但没有成功.

推荐答案

我对您的代码有几个问题:

  1. 您可以对低级客户端使用高级语法.花点时间熟悉一下different available clients
  2. 您使用的是SDK V2,而SDK V3是当前的默认版本,它允许您的应用程序更精简,因为它是模块化的:
const { DynamoDBClient } = require("@aws-sdk/client-dynamodb");
const { DynamoDBDocumentClient, BatchWriteCommand } = require("@aws-sdk/lib-dynamodb");

const client = new DynamoDBClient({});
const ddbDocClient = DynamoDBDocumentClient.from(client);

async function batchGetItems() {
 
  try {
    return await ddbDocClient.send(
        new BatchGetCommand({
          RequestItems: {
            RequestTable: {
              Keys: [
                {PK: 'Request#10001', 'SK': 'Client'},
                {PK: 'Request#10002', 'SK': 'Client'}
              ],
            },
          },
        })
    );
  } catch (err) {
    console.error(err);
  }
}

batchGetItems()
    .then((data) =>
        console.log("BatchGetCommand succeeded:", JSON.stringify(data, null, 2)))
    .catch((error) => console.error(JSON.stringify(error, null, 2)));
  1. 无论您是否使用SDK V2/V3,参数语法都是相同的,因此您可以使用与我为V2 DocumentClient提供的参数相同的语法.

Node.js相关问答推荐

MongoDB-如何验证Document字段以仅允许特定的文件扩展名?

在导入时未找到Pupeteer-PAGE-Proxy包

Sequelize-测试使用虚拟场更新模型

请求正文未定义

Nestjs swc 错误:找不到模块项目路径/src/app.module

PM2 是否需要成为其托管项目的依赖项?

使用更新版本仍然找到包@angular/fire但不支持原理图

错误:0308010C:try 通过 Github 推送部署到 firebase 托管时出现数字

如何在没有云功能的情况下使用 Google PubSub

Node js中向rest服务发送https请求的步骤

`npm install` 以Killed结尾

如何正确使用 package.json 中的关键字属性?

如何以编程方式检测nodejs中的调试模式?

为什么 JavaScript 的 parseInt(0.0000005) 打印5?

从 CoffeeScript 中的数组中删除一个值

Mongoose:查找、修改、保存

gyp WARN EACCES 用户root没有访问开发目录的权限

使用 node.js 循环 JSON

webpack-dev-server 找不到模块webpack

Node.js 中的 PHP exit()/die() 类似功能是什么