1

This question is a follow up question, the link to the other question is -- aws lambda function async connection query

I am developing server code using AWS Lambda and to test the asynchronous connection I am using the same code provided by Trung with improvement by giankotarola and the output of the Lambda Function comes only if I replace --

1. return callback(null, {body: JSON.stringify(result),statusCode:200});

with --

2. return result;

Also, at the following line --

3. pool.getConnection((err, connection) => {

It gives a warning: Expected Error to be handled

I cannot understand why the code at Line 1. mentioned above doesn't work ? And how to handle the warning at 3.

1
  • 3
    It would be helpful if you can post some code directly here as part of the question. Commented Feb 15, 2020 at 3:16

1 Answer 1

1

The first issue is because the function is using async. With async functions you just return the result. The callback is used when you are not using async.

I believe the second issue is because the err on that call isn't handled. There is a handler for the second err, but not this one. Basically that code should look like this:

let getOrder = async (sql, params) => {
    return new Promise((resolve, reject) => {
        pool.getConnection((err, connection) => {
            if (err){
                reject(err);
            }
            else {
                connection.query(sql, params, (err, results) => {
                    if (err){
                        reject(err);
                    }
                    else {
                        console.log("-----Query Done!");
                        connection.release();
                        console.log("-----Data: ", results);
                        resolve(results);
                    }
                }
            });
        });
    });
};
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Jason. I did not see the error handling for the second part. After handling the err, the warning disappears.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.