Mysql: ReferenceError: err is not defined

Created on 23 Jan 2016  路  8Comments  路  Source: mysqljs/mysql

How to fix this?

/myproject/node_modules/mysql/lib/protocol/Parser.js:77
        throw err; // Rethrow non-MySQL errors
        ^

ReferenceError: err is not defined
    at Query._callback (database.js:37:13)
    at Query.Sequence.end (/myproject/node_modules/mysql/lib/protocol/sequences/Sequence.js:96:24)
    at Query._handleFinalResultPacket (/myproject/node_modules/mysql/lib/protocol/sequences/Query.js:144:8)
    at Query.OkPacket (/myproject/node_modules/mysql/lib/protocol/sequences/Query.js:78:10)
    at Protocol._parsePacket (/myproject/node_modules/mysql/lib/protocol/Protocol.js:280:23)
    at Parser.write (/myproject/node_modules/mysql/lib/protocol/Parser.js:73:12)
    at Protocol.write (/myproject/node_modules/mysql/lib/protocol/Protocol.js:39:16)
    at Socket.<anonymous> (/myproject/node_modules/mysql/lib/Connection.js:96:28)
    at emitOne (events.js:77:13)
    at Socket.emit (events.js:169:7)

Calling code:

  let dfd = q.defer();
  db.query(
    "UPDATE `markets` SET `rate` = ? WHERE `id` = ?",
    [rate, market.id],
    (error, results, fields) => {
      debug.log(1, error);
      debug.log(1, results);
      debug.log(1, fields);

      if (err) {
        dfd.reject(err);
        return;
      }
      dfd.resolve();
    }
  );

Version: 2.10.2
Node: v5.0.0

question

Most helpful comment

@bitsofinfo it was not a problem in the library. It was a problem in my code. However, I don't know why it shows like it's a problem with Mysql.

Check your code, and follow what @dougwilson replied.

All 8 comments

Found. It was a thrown Exception from my own code that got rerouted through your error handlers.

Hi @jublo, yep, you got it! Sorry for the confusion that Node.js creates when an error is re-thrown; the first line is the line of the throw vs the line of where the error occurred (the first one in the stack trace).

I have the same issue. But I am still not knowing how to handle the exception. Is there anyone tell me?

/root/demo/node_modules/mysql/lib/protocol/Parser.js:77
        throw err; // Rethrow non-MySQL errors
        ^
Error: Received packet with no active sequence.

^^ +1 same here...

This is happening when I throw an exception from inside a mysql.connection.query callback

I normally have this in my express code to handle errors and send them back.

app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  // res.render('error', {
  //   message: err.message,
  //   error: {}
  // });
  logger.log("error", err);
  res.json({
    error: err.message,
  });
});

Now, whenever I throw something from inside the mysql query callback I get this error.

For example:

db.connection.query(query, function(err, results) {
  if(err) throw "unable to process query";
})

You can't throw from inside callback functions in Node.js, since there is no way to catch it. Node.js only gives you two options:

  1. Be very careful not to throw any errors.
  2. Wrap the full contents of your callback function body in a try-catch.

@dougwilson: Sorry, I understand it doesn't belong here but in that case, how would I bubble up an error in a way that it reaches express middleware?

@atif089 how did you solve your problem, I am having the same issue. ....

@bitsofinfo it was not a problem in the library. It was a problem in my code. However, I don't know why it shows like it's a problem with Mysql.

Check your code, and follow what @dougwilson replied.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nanom1t picture nanom1t  路  3Comments

Axxxx0n picture Axxxx0n  路  3Comments

Rhapsody-Sky picture Rhapsody-Sky  路  3Comments

johnrc picture johnrc  路  3Comments

winzig picture winzig  路  4Comments