node.jslambdaaws-lambdaclaudiajs

AWS Lambda Node - restart after interruption


I've a strange problem, I have a backend in aws lambda based on claudia.js api builder.. I've discovered a strange thing, when I register as a new user and the scripts is rejected the lambda retrieve error and stop the execution and in DB no new user is created. But when I fix the data and resubmit the registration on db I see two new user, one with the correct data and one with the previous wrong data, seems that a new lambda execution start from the end of the previous after the reject. Here some example code:

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 } });

Practical example:

1° attempt user: self (forbidden) email:whatever Register -> error, forbidden user -> no db row added

2° attempt user: newUser (permitted) email:whatever Register -> ok -> on db I find two new user: newUser and self


Solution

  • you need to return the lambda function after rejection and please set

    context.callbackWaitsForEmptyEventLoop = false;