Search code examples
javascriptnode.jsnode-mysql

MySQL Node JS Async Issues


I think I'm having some async issues with implementing a function that gets all rows from a table in a mysql database using node js. I'm using the node-mysql module.

I have already googled this, and have tried doing the accepted answer on this question says but still no luck. It tells me undefined is not a function on the throw err. Anyone know what the issue here is?

var express = require('express');
var mysql = require('mysql');
var router = express.Router();

router.get('/people', function(req, res, next) {
    getAllPeople(function(err, people) {
        res.json(people);
    });
});

function getAllPeople(cb) {
    var connection = mysql.createConnection({
        host     : 'localhost',
        user     : 'root',
        password : 'root',
        database : 'people'
    });
    connection.connect();
    connection.query('SELECT * from people', function(err, rows, fields) {
        connection.close();
        cb(err, rows);
    });
}
module.exports = router;

Solution

  • There is no close() method defined in connection object. Use connection.end()