Peter Boomsma Peter Boomsma - 3 months ago 11
MySQL Question

Getting data from MySql through express

I've set up a database on Heroku and I've created a table called users with 2 records, and now I'm trying to get the data into my Node application through express.

I've set up an connection like so in my app.js file:

// connect to the heroku database
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'us-cdbr-iron-***-**.cleardb.net',
user : 'bfe4***0ede74',
password : '6742****',
database : 'heroku_****ee0f0e9102'
});


I then have a routes folder with an index.js file:

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

/* GET home page. */
router.get('/', function(req, res, next) {
// res.render('layout', { title: 'Movieseat' });

connection.connect();

connection.query('SELECT * FROM `users` WHERE `first_name` = "Kees"', function (error, results, fields) {
// error will be an Error if one occurred during the query
// results will contain the results of the query
// fields will contain information about the returned results fields (if any)
console.log(results);
});

connection.end();
});

module.exports = router;


In this index route I'm trying to serve the record of first_name
Kees
. But when I visit my host I get the following error:


connection is not defined

ReferenceError: connection is not defined


So it looks like
connection
has no reference in my route file, but in my WebStorm IDE when I ctrl + click on the connection I get my app.js file where I define my connection. So how do I reference connection in my route file?

Also when I uncomment the following line in my index.js route file:

res.render('layout', { title: 'Movieseat' });


I get the error:


Error: Can't set headers after they are sent. What would be the propper way to request data and render a jade template?

Answer

The second error is likely because somewhere you're calling res.send() or res.end() or res.render() already, you just don't realize it. Check your middleware and so on to make sure you're not doing so.

The first issue is because you're neither exporting the connection object from your connection file, nor requiring it in your router file. You always have to explicitly require a module in order to have reference to it.

// connect to the heroku database
var mysql = require('mysql');
var connection = mysql.createConnection({
  host     : 'us-cdbr-iron-***-**.cleardb.net',
  user     : 'bfe4***0ede74',
  password : '6742****',
  database : 'heroku_****ee0f0e9102'
});
module.exports = connection;

NOte that in that case, you will always have the same connection, which isn't great for scalability. Have a look at connection pooling and consider exporting a method that gets the connection rather than passing around a global object.

Then in your router:

var express = require('express');
var router = express.Router();
var connection = require('./path/to/your/connection.js');

/* GET home page. */
router.get('/', function(req, res, next) {
  // res.render('layout', { title: 'Movieseat' });

  connection.connect();

    connection.query('SELECT * FROM `users` WHERE `first_name` = "Kees"', function (error, results, fields) {
      // error will be an Error if one occurred during the query
      // results will contain the results of the query
      // fields will contain information about the returned results fields (if any)
      console.log(results);
    });

  connection.end();
});

module.exports = router;