[英]Using DataMapper to Get Item From DynamoDB Throws Runtime Error
I am using Typescript to implement a Lambda, that connects to a Dynamo db table in the same region.我正在使用 Typescript 来实现 Lambda,它连接到同一区域中的 Dynamo 数据库表。 When I try to scan or get items from the table, I'm getting the following errors.
当我尝试扫描或从表中获取项目时,出现以下错误。
For Scan:对于扫描:
2023-01-04T20:02:56.603Z 1ef44a6b-8b4f-51c5-bd10-164456efc703 ERROR Invoke Error { "errorType": "TypeError", "errorMessage": "this.client.scan(...).promise is not a function", "stack": [ "TypeError: this.client.scan(...).promise is not a function", " at ScanPaginator.getNext (/var/task/node_modules/@aws/dynamodb-query-iterator/build/ScanPaginator.js:17:18)", " at /var/task/node_modules/@aws/dynamodb-query-iterator/build/DynamoDbPaginator.js:60:26" ] }
2023-01-04T20:02:56.603Z 1ef44a6b-8b4f-51c5-bd10-164456efc703 错误调用错误 { "errorType": "TypeError", "errorMessage": "this.client.scan(...).promise 不是函数”,“堆栈”:[“TypeError:this.client.scan(...)。promise 不是函数”,“在 ScanPaginator.getNext(/var/task/node_modules/@aws/dynamodb-query- iterator/build/ScanPaginator.js:17:18)", " at /var/task/node_modules/@aws/dynamodb-query-iterator/build/DynamoDbPaginator.js:60:26" ] }
For Get:忘记:
{ "errorType": "Runtime.UnhandledPromiseRejection", "errorMessage": "TypeError: _b.map is not a function", "reason": { "errorType": "TypeError", "errorMessage": "_b.map is not a function", "stack": [ "TypeError: _b.map is not a function", " at /var/task/node_modules/@aws-sdk/middleware-user-agent/dist-cjs/user-agent-middleware.js:14:151", " at async /var/task/node_modules/@aws-sdk/middleware-logger/dist-cjs/loggerMiddleware.js:5:22" ] }, "promise": {}, "stack": [ "Runtime.UnhandledPromiseRejection: TypeError: _b.map is not a function", " at process. (file:///var/runtime/index.mjs:1194:17)", " at process.emit (node:events:513:28)", " at emit (node:internal/process/promises:149:20)", " at processPromiseRejections (node:internal/process/promises:283:27)", " at process.processTicksAndRejections (node:internal/process/task_queues:96:32)" ] }
{ “errorType”:“Runtime.UnhandledPromiseRejection”,“errorMessage”:“TypeError:_b.map 不是函数”,“reason”:{ “errorType”:“TypeError”,“errorMessage”:“_b.map 不是函数”函数”,“堆栈”:[“TypeError:_b.map 不是函数”,“在 /var/task/node_modules/@aws-sdk/middleware-user-agent/dist-cjs/user-agent-middleware .js:14:151", " at async /var/task/node_modules/@aws-sdk/middleware-logger/dist-cjs/loggerMiddleware.js:5:22" ] }, "promise": {}, " stack": [ "Runtime.UnhandledPromiseRejection: TypeError: _b.map 不是一个函数", " at process. (file:///var/runtime/index.mjs:1194:17)", " at process.emit (节点:事件:513:28)”,“在发射(节点:内部/进程/承诺:149:20)”,“在processPromiseRejections(节点:内部/进程/承诺:283:27)”,“在进程。 processTicksAndRejections (node:internal/process/task_queues:96:32)" ] }
I'm using the DataMapper from @aws/dynamodb-data-mapper
along with @aws-sdk/client-dynamodb
in my Handler code to read from the Dynamo DB table.我在处理程序代码中使用
@aws/dynamodb-data-mapper
中的 DataMapper 和@aws-sdk/client-dynamodb
来读取 Dynamo DB 表。
Below is my DTO code:下面是我的 DTO 代码:
@table("Configs")
export class Configs {
@hashKey()
configId!: string;
@attribute()
groupName!: string
}
Below is the Lambda Handler code:以下是 Lambda 处理程序代码:
import {SQSEvent} from "aws-lambda";
import {DynamoDB} from "@aws-sdk/client-dynamodb"
import {DataMapper} from "@aws/dynamodb-data-mapper";
import {Configs} from "./models/Configs";
const client = new DynamoDB({region: "us-east-1"});
const mapper = new DataMapper({client});
export const handler = async (sqsEvent: SQSEvent) => {
console.log(`Event: ${JSON.stringify(sqsEvent, null, 2)}`);
for (let record of sqsEvent.Records) {
let messageBody = record.body;
console.log("MessageBody: " + messageBody);
}
const config = new Configs();
for await (const item of mapper.scan(Configs)) {
console.log('scan result', item);
}
console.log("done scanning");
mapper.get(Object.assign(new Configs(), {id: 'MY_ID'}))
.then(myItem => {
console.log('MyItem from db: ', myItem);
})
.catch(err => {
console.log("Not found");
})
};
Below is my tsconfig.json下面是我的 tsconfig.json
{
"compilerOptions": {
"experimentalDecorators": true,
"target": "es2020",
"strict": true,
"preserveConstEnums": true,
"outDir": "./dist/lib",
"module": "CommonJS",
"declaration": true,
"declarationDir": "./dist/types",
"sourceMap": false,
"moduleResolution":"node",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"emitDecoratorMetadata": true,
"typeRoots": [
"./node_modules/@types"
]
},
"exclude": [
"./node_modules", "**/*.test.ts"]
}
What am I doing wrong here?我在这里做错了什么? I referred to the documentation here on DataMapper https://awslabs.github.io/dynamodb-data-mapper-js/packages/dynamodb-data-mapper/ and still couldn't figure out what I'm doing wrong.
我在 DataMapper https://awslabs.github.io/dynamodb-data-mapper-js/packages/dynamodb-data-mapper/上参考了此处的文档,但仍然无法弄清楚我做错了什么。 Would greatly appreciate any help.
非常感谢任何帮助。 Thanks in advance.
提前致谢。
I believe this is because you are importing AWS SDK JS V3 for DynamoDB:我相信这是因为您正在为 DynamoDB 导入 AWS SDK JS V3:
import {DynamoDB} from "@aws-sdk/client-dynamodb"
Whereas the dynamodb-data-mapper is built for SDK V2.而 dynamodb-data-mapper 是为 SDK V2 构建的。 You should install a V2 client for DynamoDB:
您应该为 DynamoDB 安装 V2 客户端:
import DynamoDB = require('aws-sdk/clients/dynamodb');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.