0

我有一个奇怪的问题,我有一个基于 claudia.js api builder 的 aws lambda 后端。我发现了一个奇怪的事情,当我注册为新用户并且脚本被拒绝时,lambda 检索错误并停止执行并且在数据库中没有创建新用户。但是当我修复数据并在 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(允许)电子邮件:whatever 注册 -> ok -> 在 db 上我找到两个新用户:newUser 和 self

4

2 回答 2

1

您需要在拒绝后返回 lambda 函数,请设置

context.callbackWaitsForEmptyEventLoop = false;

于 2019-08-23T14:19:11.057 回答
0

对于那些有同样问题的人来说,似乎return;拒绝后的设置解决了问题。

于 2019-08-23T09:35:40.613 回答