简体   繁体   中英

TypeError: Cannot read property 'id' of undefined - AWS Lambda

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

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.

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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM