2

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;

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

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.