I have just finished building the react note app from aws.
https://aws.amazon.com/getting-started/hands-on/build-react-app-amplify-graphql/module-one/?nc1=h_ls
For practice purpose, I want to replace those functions:fecthNotes, createNotes and deleteNotes in App.js using lambda function. However, I am totally new to javascript and all the things I wrote are generated by chatGPT. I successfully replaced fecthNotes, but I meet some problems in createNotes.
import awsDynamoDB from '@aws-sdk/client-dynamodb';
import { v4 as uuidv4 } from 'uuid';
const { DynamoDBClient, PutCommand } = awsDynamoDB;
const client = new DynamoDBClient({ region: "us-east-2" });
export const handler = async (event) => {
if (!event.body) {
return {
statusCode: 400,
body: JSON.stringify({ message: "No data provided" }),
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
}
};
}
let data;
try {
data = JSON.parse(event.body);
} catch (error) {
return {
statusCode: 400,
body: JSON.stringify({ message: "Invalid JSON input", errorMessage: error.message }),
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
}
};
}
const { name, description, image } = data;
const item = {
id: uuidv4(),
name,
description,
image,
createdAt: new Date().toISOString()
};
const params = {
TableName: 'Note-li2tepohrvffzahbnswzdygzae-dev',
Item: item
};
try {
await client.send(new PutCommand(params));
return {
statusCode: 200,
body: JSON.stringify({ message: "Note created successfully", note: item }),
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
}
};
} catch (error) {
console.error('Error creating the note:', error);
return {
statusCode: 500,
body: JSON.stringify({ message: "Failed to create the note", errorMessage: error.message, errorStack: error.stack }),
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
}
};
}
};
{
“statusCode”: 500,
“body”: “{“message”:”Failed to create the note”,”errorMessage”:”PutCommand is not a constructor”,”errorStack”:”TypeError: PutCommand is not a constructorn at Runtime.handler (file:///var/task/index.mjs:50:27)n at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29)”}”,
“headers”: {
“Content-Type”: “application/json”,
“Access-Control-Allow-Origin”: “*”
}
}