mysql icon indicating copy to clipboard operation
mysql copied to clipboard

Cannot read property 'query' of undefined in node.js server(+MySQL)

Open HwangJaeYoung opened this issue 11 years ago • 11 comments

I have a some problem about node.js server. What should I do to solve the this problem?

Below list is error message to node.js server(+MySQL):

TypeError: Cannot read property 'query' of undefined
at c:\Users\Administrator\Mobius Yellow Turtle\app.js:202:15
at Handshake.onConnect (c:\Users\Administrator\Mobius Yellow Turtle\node_modules\mysql\lib\Pool.js:54:9)
at Handshake.Sequence.end (c:\Users\Administrator\Mobius Yellow Turtle\node_modules\mysql\lib\protocol\sequences\Sequence.js:96:24)
at Handshake.ErrorPacket (c:\Users\Administrator\Mobius Yellow Turtle\node_modules\mysql\lib\protocol\sequences\Handshake.js:103:8)
at Protocol._parsePacket (c:\Users\Administrator\Mobius Yellow Turtle\node_modules\mysql\lib\protocol\Protocol.js:274:23)
at Parser.write (c:\Users\Administrator\Mobius Yellow Turtle\node_modules\mysql\lib\protocol\Parser.js:77:12)
at Protocol.write (c:\Users\Administrator\Mobius Yellow Turtle\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.<anonymous> (c:\Users\Administrator\Mobius Yellow Turtle\node_modules\mysql\lib\Connection.js:96:28)
at Socket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)

HwangJaeYoung avatar Nov 21 '15 15:11 HwangJaeYoung

Hi! This is an error your your code, and the error is coming from the logic you have in app.js line 202. We should be able to help you if you share that entire file with us, so we can figure out where the mistake is :)

dougwilson avatar Nov 21 '15 16:11 dougwilson

I have similar error. Here is my function which executes mysql queries using pool connection

const pool = mysql.createPool(config.get('mysql'));

function execute(query) {

  return new Promise((resolve, reject) => {

    pool.getConnection((err, connection) => {
      if (err) {
        logger.error("Error getting connection from MySQL pool");
        reject(err);
      }

      connection.query(query, (err,rows) => {
        if (err) {
          logger.error("Couldn't execute SQL query");
          reject(err);
        }
        connection.release();

        resolve(rows[0]);
      });
    });
  });
}

but I get an error

connection.query(query, (err,rows) => { TypeError: Cannot read property 'query' of undefined

which comes after checking if there was an error in getting connection from the pool. Question is in what kind of situations there is no error while you get connection from the pool, but connection object itself is undefined?

Satyricon avatar Aug 17 '16 12:08 Satyricon

Hi @Satyricon , you check for the error and call reject(err), but then you don't actually stop your code from continuing to execute. You need to either wrap the connection.query in an else block or add return; as the last statement of your if block.

dougwilson avatar Aug 17 '16 12:08 dougwilson

Good catch, but the problem is that I never get error log that there was a problem in getting connection from the pool.

Satyricon avatar Aug 17 '16 12:08 Satyricon

Hm, weird, @Satyricon. I'm not sure how much I can really help with the invormation provided. Can you open a new issue for this? Also, please include all the information I would need to be able to reproduce the issue, or I really have no idea why you would not get an error nor a connection object. Have you been able to attach a debugger and see what is happening in the code?

dougwilson avatar Aug 17 '16 12:08 dougwilson

I am running this code in very concurrent environment, that's why it is hard reproduce this error without enough load on the system. Shortly I am reading JSONs from kafka queue and for some key/value pairs from JSON making sql queries to add more data to JSON. So I can have many simultaneous sql queries to DB. In mysql conf file I have a limit for pool connection amount (100), but I am not setting waitForConnections parameter to false (and default value is true), so it should queue for getting connections from pool.

Satyricon avatar Aug 17 '16 12:08 Satyricon

And comment to your first comment @dougwilson related to return after rejecting promise. Here is a test function:

function test(arg) {
 return new Promise((res, rej) => {
   if (arg > 10) {
     console.log("before reject")
     reject("err")
     console.log("after reject")
   }

   console.log("before resolve")
   resolve("suc")
   console.log("after resolve")
 })
}

test(14).then((res) => console.log(res))

and result is

before reject

so I don't think that you need to terminate with return statement after rejection.

Satyricon avatar Aug 17 '16 12:08 Satyricon

Ah, @Satyricon, good to know. I don't really use promises, so didn't know :) I'm truly at a loss, though, I have no idea how to help you...

dougwilson avatar Aug 17 '16 12:08 dougwilson

Perhaps, if you could offer up a pull request that fixes the issue? I could certainly get it merged & released :) !

dougwilson avatar Aug 17 '16 12:08 dougwilson

First I need to figure out what is happening :). In all cases thanks for trying to help.

Satyricon avatar Aug 17 '16 12:08 Satyricon

No problem, @Satyricon ! Sorry I couldn't help :( If you could provide enough information for me to reproduce it locally, I can attach a debugger and track it down in no time...

dougwilson avatar Aug 17 '16 12:08 dougwilson