I'm trying to implement a function which is called by API Gateway. It gets passed a email address+password, then checks if the Email address is already in use. If thats not the case it should be put in my dynamo DB table.
When testing it with an Email address which is already in use, the put operation is still executed, eventhough the boolean should be set to true.
'use strict';
var AWS = require('aws-sdk'),
uuid = require('uuid'),
documentClient = new AWS.DynamoDB.DocumentClient();
exports.handler = function(event, context, callback) {
if (event.body !== null && event.body !== undefined) {
let body = JSON.parse(event.body);
let eMailAddress = body.mail;
let password = body.password;
var EmailInUse = Boolean(false);
var paramsScan = {
TableName: "accounts"
};
documentClient.scan(paramsScan, function(err, data) {
for (var i in data.Items) {
i = data.Items;
if (i.EmailAddress == eMailAddress) {
console.log("already used");
callback(err, "Email Address already in Use!");
EmailInUse = true;
}
}
});
console.log(EmailInUse);
if (EmailInUse == false) {
console.log("should not enter if email used");
var params = {
Item: {
"AccountID": uuid.v1(),
"Password": password,
"EmailAddress": eMailAddress
},
TableName: "accounts"
};
documentClient.put(params, function(err, data) {
if (err) {
callback(err, null);
} else {
const response = {
statusCode: "200",
"headers": {},
body: JSON.stringify(params),
"isBase64Encoded": "false"
};
callback(null, response);
}
});
}
}
};
this is my Cloudwatch log for calling it 2 times with the same parameters:
12:54:01
START RequestId: 281b0eda-950b-40fc-a2e2-d326cd04f8a4 Version: $LATEST
12:54:01
2019-02-26T12:54:01.434Z 281b0eda-950b-40fc-a2e2-d326cd04f8a4 false
12:54:01
2019-02-26T12:54:01.471Z 281b0eda-950b-40fc-a2e2-d326cd04f8a4 should not enter if email used
12:54:01
END RequestId: 281b0eda-950b-40fc-a2e2-d326cd04f8a4
12:54:01
REPORT RequestId: 281b0eda-950b-40fc-a2e2-d326cd04f8a4 Duration: 320.98 ms Billed Duration: 400 ms Memory Size: 128 MB Max Memory Used: 31 MB
12:54:47
START RequestId: b9df94ce-0d59-4dfb-8b61-8098db566431 Version: $LATEST
12:54:47
2019-02-26T12:54:47.591Z b9df94ce-0d59-4dfb-8b61-8098db566431 false
12:54:47
2019-02-26T12:54:47.591Z b9df94ce-0d59-4dfb-8b61-8098db566431 should not enter if email used
12:54:47
2019-02-26T12:54:47.812Z b9df94ce-0d59-4dfb-8b61-8098db566431 already used
12:54:47
END RequestId: b9df94ce-0d59-4dfb-8b61-8098db566431
12:54:47
REPORT RequestId: b9df94ce-0d59-4dfb-8b61-8098db566431 Duration: 311.87 ms Billed Duration: 400 ms Memory Size: 128 MB Max Memory Used: 31 MB
Looking at this i notice that the last log output "already used" is called after the check if the Email address is already in use. Can somebody tell me how to resolve this problem? Many thanks in advance.
mail,eMailAddress, andEmailAddress. I would pick one and go with it to make your life a little easier!