【发布时间】:2019-07-20 01:08:05
【问题描述】:
我正在尝试实现一个由 API Gateway 调用的函数。它传递了一个电子邮件地址+密码,然后检查该电子邮件地址是否已在使用中。如果不是这样,它应该放在我的发电机数据库表中。
当使用已使用的电子邮件地址对其进行测试时,尽管布尔值应设置为 true,但仍会执行 put 操作。
'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);
}
});
}
}
};
这是我的 Cloudwatch 日志,使用相同的参数调用了 2 次:
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
看着这个我注意到最后一个日志输出“已经使用”在检查电子邮件地址是否已被使用之后被调用。有人可以告诉我如何解决这个问题吗?非常感谢。
【问题讨论】:
-
我假设您正在对密码进行哈希处理,但只是没有将其包含在您的代码示例中。
-
您将电子邮件地址称为
mail、eMailAddress和EmailAddress。我会选择一个并使用它,让您的生活更轻松!
标签: javascript amazon-web-services aws-lambda amazon-dynamodb aws-api-gateway