Borda
Borda

Reputation: 139

Undefined query function

so I'm currently using the following code to execute queries;

var mysql = require('mysql');

var config = require('./config');
var pool = mysql.createPool(config.mysql);

function query(statement){
  return new Promise(function(resolve, reject){
    pool.getConnection(function(err, connection) {
      if(err) reject(err);

      connection.query(statement, function(err, row){
        connection.release();

        if(err){
          reject(err);
        }

        resolve(row);        
      })
    });
  });
}

module.exports = {
  pool: pool,
  query: query
};

Whenever the query function is called it results in a undefined error;

TypeError: Cannot read property 'query' of undefined

I'm quite out of ideas why this could be, this would be connection.getConnection is not returning a proper connection, would this mean my credentials are wrong in my createPool function?

config.mysql

mysql: {
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'site',
    connectionLimit : 10,           
    multipleStatements : true  
  }

Upvotes: 0

Views: 1068

Answers (1)

Charlie Fish
Charlie Fish

Reputation: 20526

The problem is on the following line you are passing in the query function when you should be passing in a string.

connection.query(query, function(err, row){

query in that case should be a string. But you defined query as a function (function query(){)

If you change the line to something LIKE the following this should work.

connection.query("SELECT * from Users", function(err, row){

Upvotes: 1

Related Questions