2
votes

I have tried all the solutions found on other similar stack overflow questions but I still get this error. I'm a beginner and have only just started learning this. So, I'm not sure if I'm following the solutions right. But here's my code. I would appreciate it if someone could help me.

var mysql = require('mysql');
var pool  = mysql.createPool({
host     : 'host',
user     : 'user',
password : 'password',
database : 'student',
connectionLimit : 10,               
  multipleStatements : true
});


exports.handler =  function(event, context, callback) {


  context.callbackWaitsForEmptyEventLoop = false;

  pool.getConnection(function(err, connection) {

  connection.query("SELECT name FROM 'student'.'course'", function (err, results, fields) {

      if (err) {
        console.log("NOT-CONNECTED!");
        connection.destroy();
        throw err;
    } else {
        // connected!
        console.log("CONNECTED!");
        console.log(results);
        callback(err, results);
        connection.end(function (err) { callback(err, results);});
    }
    });
 });
};
1
Have you tried increasing the timeout or memory from default settings under Basic settings? Also, are any of your console.log messages actually executing? - Parrett Apps
The message "Process exited before completing request" means that the Javascript function exited before calling context.done (or context.succeed, callback etc.). You can try increase function's timeout (default is 6s), I think the getConnection function takes more time, you can add console.log step by step to detect the issue. - hoangdv

1 Answers

0
votes

In your error scenario, you are never calling callback. You'll need to do something like -

if (err) {
        console.log("NOT-CONNECTED!");
        connection.destroy();
        callback(null, err)// or callback(err)
    }