0

I have a lambda function that makes a call to the dynamodb table using pk and sk. For some reason I am getting the return data as undefined. Upon checking the cloudwatch logs I see the below error messages. What could be the typo I am doing here?

2021-10-01T00:15:03.104Z cdd7201f-0c95-4283-9257-c07324998896 INFO BOL Data: undefined

2021-10-01T00:15:03.124Z cdd7201f-0c95-4283-9257-c07324998896 INFO BOL Error: TypeError: Cannot read property 'PK' of undefined at getUserById (/var/task/getUserById.js:16:27) at processTicksAndRejections (internal/process/task_queues.js:95:5)

Here is the lambda code the error is referring to -

const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient();

async function getUserById(userId) {
    console.log('USERID:',userId);
    const params = {
        TableName:"Bol",
        KeyConditionExpression: 'PK = :hashKey and SK = :sortKey',
        ExpressionAttributeValues: {
            ':hashKey': userId,
            ':sortKey': 'USER'
        }
    };
    try {
        const { Item } = await docClient.query(params).promise();
        console.log('BOL Data:',Item);
        return { id: Item.PK, name: Item.Data.displayName };
    } catch(err) {
        console.log("BOL Error: ", err);
    }
 }
 
 module.exports = getUserById;

Below is the data I am supposed to receive on lambda - enter image description here

1 Answer 1

1

Its the way you are Initialization the Item

you can try using the below methods to get the objects

 try {
        const Item  = await docClient.query(params).promise();
        Item.Items.forEach(function(item) {
            let buffer=item.Data+" -- "+item.PK;
           console.log("buffer ",buffer)
        });
        
    } catch(err) {
        console.log("BOL Error: ", err);
    }

There's are multiple ways you can get the favourable response refer below:

Making Requests with the Document Client

Formatting DynamoDB data to normal JSON in AWS Lambda

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Ruben, your answer got me thinking in the right direction

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.