question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

uncatchable `uncaughtException` in `pool.execute` if `undefined` in query values

See original GitHub issue

undefined in query values will cause TypeError: Bind parameters must not contain undefined. To pass SQL NULL specify JS null

with pool.execute, is there a place to catch the error?

the simplified test script:

// test-pool-execute-error.js

const Mysql = require('mysql2')

const CONFIG = {
  host: '127.0.0.1',
  port: 3306,
  user: 'root',
  database: 'mysql'
}

const testNormal = () => {
  console.log('[testNormal] start')
  const pool = Mysql.createPool(CONFIG)
  pool.execute(
    `SET @value = ?`,
    [ 0 ], // good value
    (error, resultList, fieldList) => console.log('[testNormal] from callback:', { error, resultList, fieldList })
  )
  setTimeout(() => pool.end(() => console.log('[testNormal] pool ended')), 1000)
}

const testError = () => {
  try {
    console.log('[testError] start')
    const pool = Mysql.createPool(CONFIG)
    pool.on('error', (error) => console.log('[testError] error from event', error))
    pool.execute(
      `SET @value = ?`,
      [ undefined ], // bad value
      (error, resultList, fieldList) => console.log('[testError] from callback:', { error, resultList, fieldList })
    )
    setTimeout(() => pool.end(() => console.log('[testError] pool ended')), 1000)
  } catch (error) { console.log('[testError] error from catch', error) }
}

process.on('uncaughtException', (error) => console.error('!!! error from uncaughtException', error))

testNormal()

setTimeout(() => { // wait first test pass
  testError()
}, 2000)

the output with mysql2@1.6.4: (testNormal pass, but testError throws and block the process exiting)

$ node test-pool-execute-error.js
[testNormal] start
[testNormal] from callback: { error: null,
  resultList:
   ResultSetHeader {
     fieldCount: 0,
     affectedRows: 0,
     insertId: 0,
     info: '',
     serverStatus: 2,
     warningStatus: 0 },
  fieldList: undefined }
[testNormal] pool ended
[testError] start
!!! error from uncaughtException TypeError: Bind parameters must not contain undefined. To pass SQL NULL specify JS null
    at ...\node_modules\mysql2\lib\connection.js:609:17
    at Array.forEach (<anonymous>)
    at PoolConnection.execute (...\node_modules\mysql2\lib\connection.js:607:22)
    at ...\node_modules\mysql2\lib\pool.js:171:31
    at Pool.<anonymous> (...\node_modules\mysql2\lib\pool.js:68:18)
    at PoolConnection.<anonymous> (...\node_modules\mysql2\lib\connection.js:753:13)
    at Object.onceWrapper (events.js:273:13)
    at PoolConnection.emit (events.js:182:13)
    at ClientHandshake.<anonymous> (...\node_modules\mysql2\lib\connection.js:108:20)
    at ClientHandshake.emit (events.js:182:13)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

15reactions
hktr92commented, Dec 14, 2020

it just happened to me, too. in a project that is supposed to go live sooner or later.

more time is wasted to overcome this issue by this method (implementing a strict type conversion + restart policy for a fallback in business logic) rather than having a try / catch in the library itself… which can be further caught and handled properly.

if the intention is to report an issue, then throw an error, don’t kill the process. it’s bad design imho and raises uncertainty for production.

also, the error reported is pretty ambiguous and doesn’t really tell what query caused this issue, which makes harder to debug + repair quickly (for reference, Doctrine (PHP library) reports which query failed in dev mode).

10reactions
damianobarbaticommented, Jan 21, 2020

Why was this closed? An unhandled rejection cause process exit/crash. This is pretty serious; there’s no way to catch this error, no matter where you put the try/catch .

@dr-js how did you solve?

Read more comments on GitHub >

github_iconTop Results From Across the Web

bind parameters must not contain undefined - You.com
Bind problem in SQL query in Node, Express, Mysql2 app ... sidorares/node-mysql2uncatchable `uncaughtException` in `pool.execute` if `undefined` in query ...
Read more >
Undefined query function - javascript - Stack Overflow
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...
Read more >
Cannot Read Property of Undefined in JavaScript - Rollbar
What Causes TypeError: Cannot Read Property of Undefined. Undefined means that a variable has been declared but has not been assigned a value....
Read more >
Uncaught TypeError: Cannot read property of undefined
After I assign a value to a, the function will return me the value of t, mapped by a=10.
Read more >
Null Pointers are another name for undefined value... - 270136
The system has attempted to use an undefined value, which usually indicates a programming error, either in your code or some system code....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found