我有一个奇怪的问题,我有一个基于claudia.js api生成器的aws lambda后端。当我注册为新用户并且脚本被拒绝lambda检索错误时,我发现了一件奇怪的事情。并停止执行,并且在DB中没有创建新用户。但是,当我修复数据并在db上重新提交注册时,我看到两个新用户,一个具有正确的数据,另一个具有先前的错误数据,似乎是新的lambda执行从拒绝后的前一个末尾开始。下面是一些示例代码:
api.post('/users/register', function(request) {
return new Promise((resolve, reject) => {
if ( typeof request.post !== 'undefined'){
if ( typeof request.post.email !== 'undefined' && typeof request.post.name !== 'undefined' && typeof request.post.password !== 'undefined' && typeof request.post.repassword !== 'undefined' ){
var userData = request.post;
userData.email = xssFilters.inHTMLData(userData.email);
userData.name = xssFilters.inHTMLData(userData.name);
userData.password = xssFilters.inHTMLData(userData.password);
userData.repassword = xssFilters.inHTMLData(userData.repassword);
}else{
reject("Missing some data");
}
if ( userData.name.toLowerCase() == 'self' || userData.name == ''){
reject("Username forbidden");
// here the scripts stop execution on error but on the next execution seems that togheter the new function start also the previous right after the reject
}
}
else{
reject("Missing POST data - application/x-www-form-urlencoded");
}
// here the db insert ect..
});
},{ success : { code : 200}, error : { code : 401 } });
实际示例:
1°尝试 用户:自我(禁止) 电子邮件:无论 注册->错误,禁止用户->未添加数据库行
2°尝试 用户:newUser(允许) 电子邮件:无论 在数据库上注册->确定->在数据库上,我找到两个新用户:newUser和self
答案 0 :(得分:1)
您需要在拒绝后返回lambda函数,请设置
context.callbackWaitsForEmptyEventLoop = false;
答案 1 :(得分:0)
对于那些有相同问题的人,拒绝后设置return;
似乎可以解决问题。