2

I got issue when working with AWS Lambda

TypeError: Cannot read property 'id' of undefined at exports.handler (/var/task/index.js:19:28)

Here is my code:

var AWS = require("aws-sdk");
var dynamoDBConfiguration = {
"region" : "us-west-2", 
"endpoint" : "dynamodb.us-west-2.amazonaws.com"
};

AWS.config.update(dynamoDBConfiguration);

var docClient = new AWS.DynamoDB.DocumentClient();


exports.handler = function(event, context, callback) {

var params = {
    TableName: "User",
    ProjectionExpression: "id, password",
    FilterExpression: "id = :id and password = :password",
    ExpressionAttributeValues: {
         ":id" : event.body.id,
         ":password" : event.body.password
    }
};

docClient.scan(params, onScan);

function onScan(err, data) {
    if (err) {
        console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2));
} 
else 
{
    console.log("Scan succeeded.");
    context.succeed(data.Items);

    // continue scanning if we have more movies
    if (typeof data.LastEvaluatedKey != "undefined") {
        console.log("Scanning for more...");
        params.ExclusiveStartKey = data.LastEvaluatedKey;
        docClient.scan(params, onScan);
    }
}
}
}

However, this afternoon, when I ran it, it ran perfectly.

Can you guys help me to fix this error?

Thanks in advance

Trung Nguyen
  • 51
  • 2
  • 4

2 Answers2

0
FilterExpression:"#id = :id and #password = :password",
    ExpressionAttributeNames: {
        "#id":"Your particular id",
        "#password":"your password"
    },

You can use the ExpressionAttributesNames also in the params of your code. I think it may be you are using ExpressionAttributesValues only.

0

It means that event.body.id is undefined. Use event.id instead of event.body.id.

Shouldn't it be like this?

ExpressionAttributeValues: {
         ":id" : event.id,
         ":password" : event.password
    }
Abdul Manaf
  • 4,933
  • 8
  • 51
  • 95