- Newest
- Most votes
- Most comments
The error you're encountering is due to an incorrect format in the Key parameter of your GetItemCommand. When using DynamoDB with JavaScript, you need to specify the data type of the key attribute. In your case, it seems that 'name' is likely a string, so you should format the Key as follows:
const getItemCommand = new GetItemCommand({ TableName: "full-stack-react-db", Key: { name: { S: name } } });
Here, 'S' indicates that the 'name' attribute is of type String. This format is required by DynamoDB to properly interpret the key values.
Additionally, when retrieving the item from DynamoDB, you might want to handle the response slightly differently. The item will be in the 'Item' property of the response. You can modify your code like this:
const response = await dynamoDBclient.send(getItemCommand); const article = response.Item; if (article) { res.json(article); } else { res.status(404).json({ error: "Article not found" }); }
This change ensures that you're sending the actual item data in the response, and it also handles the case where the item might not be found in the database.
Remember to handle any potential errors that might occur during the database operation using try-catch blocks for more robust error handling.
Sources
DynamoDB examples using SDK for JavaScript (v2) - AWS SDK Code Examples
Building Scalable and Secure Serverless Applications with AWS Lambda | AWS re:Post
Programming Amazon DynamoDB with JavaScript - Amazon DynamoDB
answered 2 years ago
Relevant content
- AWS OFFICIALUpdated 2 years ago

After making this change Key: {name : {S: name}} i am getting this error { name: 'learn-react' } 0|my_app | GetItemCommand { 0|my_app | middlewareStack: { 0|my_app | add: [Function: add], 0|my_app | addRelativeTo: [Function: addRelativeTo], 0|my_app | clone: [Function: clone], 0|my_app | use: [Function: use], 0|my_app | remove: [Function: remove], 0|my_app | removeByTag: [Function: removeByTag], 0|my_app | concat: [Function: concat], 0|my_app | applyToStack: [Function: cloneTo], 0|my_app | identify: [Function: identify], 0|my_app | identifyOnResolve: [Function: identifyOnResolve], 0|my_app | resolve: [Function: resolve] 0|my_app | }, 0|my_app | serialize: [AsyncFunction: se_GetItemCommand], 0|my_app | deserialize: [AsyncFunction: de_GetItemCommand], 0|my_app | input: { TableName: 'full-stack-react-db', Key: { name: [Object] } } 0|my_app | } 0|my_app | Error: Resolved credential object is not valid
My object is not empty { name: 'learn-react' }