Skip to content

TypeError: Cannot read properties of undefined (reading '0')

0

My Code snippet


app.get('/apimd/articles/:name', async (req, res) => { const {name} = req.params; console.log({name}); //const article = await db.collection('articles').findOne({name}); const getItemCommand = new GetItemCommand({ TableName: "full-stack-react-db", Key: { name: name } }); console.log(getItemCommand);

const article = await dynamoDBclient.send(getItemCommand);
res.json(article);

});

What is wrong?

TypeError: Cannot read properties of undefined (reading '0') 0|my_app | at Object.visit (/home/ubuntu/apps/Full-Stack_Site_with_React/back-end/node_modules/@aws-sdk/client-dynamodb/dist-cjs/index.js:1065:36) 0|my_app | at se_AttributeValue (/home/ubuntu/apps/Full-Stack_Site_with_React/back-end/node_modules/@aws-sdk/client-dynamodb/dist-cjs/index.js:2631:25) 0|my_app | at /home/ubuntu/apps/Full-Stack_Site_with_React/back-end/node_modules/@aws-sdk/client-dynamodb/dist-cjs/index.js:2907:16 0|my_app | at Array.reduce (<anonymous>) 0|my_app | at se_Key (/home/ubuntu/apps/Full-Stack_Site_with_React/back-end/node_modules/@aws-sdk/client-dynamodb/dist-cjs/index.js:2903:32) 0|my_app | at Key (/home/ubuntu/apps/Full-Stack_Site_with_React/back-end/node_modules/@aws-sdk/client-dynamodb/dist-cjs/index.js:2856:17) 0|my_app | at applyInstruction (/home/ubuntu/apps/Full-Stack_Site_with_React/back-end/node_modules/@smithy/smithy-client/dist-cjs/index.js:1123:27) 0|my_app | at take (/home/ubuntu/apps/Full-Stack_Site_with_React/back-end/node_modules/@smithy/smithy-client/dist-cjs/index.js:1091:5) 0|my_app | at se_GetItemInput (/home/ubuntu/apps/Full-Stack_Site_with_React/back-end/node_modules/@aws-sdk/client-dynamodb/dist-cjs/index.js:2852:40) 0|my_app | at se_GetItemCommand (/home/ubuntu/apps/Full-Stack_Site_with_React/back-end/node_modules/@aws-sdk/client-dynamodb/dist-cjs/index.js:1291:25)

asked 2 years ago911 views

1 Answer
-1

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

  • 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' }

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.