I’m creating an API in AWS API gateway.
Create a DB in DynamoDB and code in Lambda.
Passing the constant (id: ‘1’) works but does not when used path parameter.
I’m using nodejs22 web framework.
This is the code:
//import AWS from “@aws-sdk/lib-dynamodb”;
import { DynamoDBClient } from “@aws-sdk/client-dynamodb”;
import { DynamoDBDocumentClient, GetCommand } from “@aws-sdk/lib-dynamodb”;export const handler = async (event) => {
// Bare-bones DynamoDB Client
const client = new DynamoDBClient({});// Bare-bones document client
const ddbDocClient = DynamoDBDocumentClient.from(client); // client is DynamoDB client// Path parameters
const {pizza_id} = event.pathParameters;const params = {
TableName: ‘Pizza’,
Key: {
//id: ‘2’
id: pizza_id
}
};try {
const data = await ddbDocClient.send(new GetCommand(params));
//console.log(“Success”, data.Item);
const response = {
statusCode: 200,
body: JSON.stringify(data.Item),
};
return response;
} catch (err) {
console.error(“Error”, err);
}};
This is the error:
{
“errorType”: “TypeError”,
“errorMessage”: “Cannot destructure property ‘pizza_id’ of ‘event.pathParameters’ as it is undefined.”,
“trace”: [
“TypeError: Cannot destructure property ‘pizza_id’ of ‘event.pathParameters’ as it is undefined.”,
" at Runtime.handler (file:///var/task/index.mjs:13:10)“,
" at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1205:29)”
]
}
Thank you in advance (",)