I am using Typescript to implement a Lambda, that connects to a Dynamo db table in the same region. 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" ] }
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)" ] }
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.
Below is my DTO code:
@table("Configs")
export class Configs {
@hashKey()
configId!: string;
@attribute()
groupName!: string
}
Below is the Lambda Handler code:
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
{
"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. Would greatly appreciate any help. Thanks in advance.
CodePudding user response:
I believe this is because you are importing AWS SDK JS V3 for DynamoDB:
import {DynamoDB} from "@aws-sdk/client-dynamodb"
Whereas the dynamodb-data-mapper is built for SDK V2. You should install a V2 client for DynamoDB:
import DynamoDB = require('aws-sdk/clients/dynamodb');