Lambda Serverless Nodejs - Getting "Internal server error" when callback is called inside a promise

1.1k Views Asked by At

I am trying to call the callback(null,response) inside .then() block of a promise. It works fine when i am using serverless offline but its giving error when using serverless.

There are two scenarios:

1 Works fine (when deployed using serverless deploy and when using sls offline start)

module.exports.getAssembly = (event, context, callback) => {

        const response = {
            statusCode: 200,
            body: JSON.stringify({
                message: 'Go Serverless v1.0! Your function executed successfully!'
            }),
        };
        callback(null, response)


}

2 Works fine with sls offline but gives internal server error with serverless deploy

module.exports.getAssembly = (event, context, callback) => {


    mysql.query('SELECT * from assemblies',connection).then((returnedObject)=>{
        const response = {
            statusCode: 200,
            body: JSON.stringify({
                message: returnedObject.results
            }),
        };
        callback(null, response)

    })

}

There is some problem with the callback(null,response) inside the .then() block of the promise

1

There are 1 best solutions below

0
On BEST ANSWER

Setting context.callbackWaitsForEmptyEventLoop = false; solves the problem.