0

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  
  }
3
  • You aren't defining query anywhere. Commented Dec 28, 2018 at 18:53
  • @CharlieFish sorry? function query() Edit: I see now - this is precisely how errors can be the smallest things, thanks! Commented Dec 28, 2018 at 18:53
  • Of course! Hopefully my answer gives more context. Feel free to accept or upvote my answer if it helped. And let me know if you need me to add anymore context or information to my answer. Commented Dec 28, 2018 at 18:55

1 Answer 1

1

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){
Sign up to request clarification or add additional context in comments.

14 Comments

Somehow I'm still getting the same error; see updated post for updated function
@Borda What happens when you add console.log(statement). What does that print? Right above connection.query(statement
i.gyazo.com/312b7f0741d2b35af8a9c80618c76a72.png Somehow connection is undefined. Edit: when I log connection it returns undefined
@Borda You are correct. What happens if you rename the pool variable to pool instead of connection?
Err, I changed it to the above function (see updated post), now I'm getting a odd TypeError: Cannot read property 'acquireTimeout' of undefined error.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.