sidorares / node-mysql2

:zap: fast mysqljs/mysql compatible mysql driver for node.js
https://sidorares.github.io/node-mysql2/
MIT License
4.07k stars 619 forks source link

TypeError: undefined is not a function #612

Closed zfanta closed 7 years ago

zfanta commented 7 years ago

I wrote query that insert a user or update on duplicate(email)

'INSERT INTO user` (`email`, `password`, `name`, `comment`) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE `password` = ?, `name` = ?, `comment` = ?'

It works well on other mysql clients, but mysql2 throws error.

code

const connection = await mysql.createConnection(Object.assign({debug: true}, config.database));
let row;
try {
  [[row]] = await connection.query('INSERT INTO `user` (`email`, `password`, `name`, `comment`) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE `password` = ?, `name` = ?, `comment` = ?', [
    user.email,
    user.password,
    user.name,
    user.comment,
    user.password,
    user.name,
    user.comment
  ]);
} catch (e) {
  connection.close();
  throw e;
}

debug log

Add command: Connection
 raw: 0a352e362e33360018030000507373682e4c752800fff72102007f801500000000000000000000644051563c6772286c6a5032006d7973716c5f6e61746976655f70617373776f726400
2 undefined ==> Connection#handshakeInit(0,,78)
Server hello packet: capability flags:2155870207=(long password, found rows, long flag, connect with db, no schema, compress, odbc, local files, ignore space, protocol 41, interactive, ignore sigpipe, transactions, reserved, secure connection, multi statements, multi results, ps multi results, plugin auth, connect attrs, plugin auth lenenc client data, can handle expired passwords, remember options)
Sending handshake packet: flags:8582095=(long password, found rows, long flag, connect with db, odbc, local files, ignore space, protocol 41, ignore sigpipe, transactions, reserved, secure connection, multi results, session track)
2 792 <== Connection#handshakeInit(1,,75)
2 792 <== 47000001cff3820000000000e00000000000000000000000000000000000000000000000726f6f7400146a960f9c7ca80ac9f599ec64a4e823b978fa80977563616e2d6170692d6e657700
 raw: 00000002000000
2 792 ==> Connection#handshakeResult(2,maybeOK,11)
Add command: query
        Sending query command: INSERT INTO `user` (`email`, `password`, `name`, `comment`) VALUES ('8123j@asd.com', '$2a$10$np5YxihqB3wl2wGHMQSXk.clRtqLLF4OC8toCAA2mNd6AklyMzC.2', 'lkasd', 'nkl') ON DUPLICATE KEY UPDATE `password` = '$2a$10$np5YxihqB3wl2wGHMQSXk.clRtqLLF4OC8toCAA2mNd6AklyMzC.2', `name` = 'lkasd', `comment` = 'nkl'
2 792 <== query#start(0,,306)
2 792 <== 2e01000003494e5345525420494e544f20607573657260202860656d61696c602c206070617373776f7264602c20606e616d65602c2060636f6d6d656e7460292056414c554553202827383132336a406173642e636f6d272c2027243261243130246e703559786968714233776c327747484d5153586b2e636c5274714c4c46344f4338746f434141326d4e6436416b6c794d7a432e32272c20276c6b617364272c20276e6b6c2729204f4e204455504c4943415445204b455920555044415445206070617373776f726460203d2027243261243130246e703559786968714233776c327747484d5153586b2e636c5274714c4c46344f4338746f434141326d4e6436416b6c794d7a432e32272c20606e616d6560203d20276c6b617364272c2060636f6d6d656e7460203d20276e6b6c27
 raw: 00012302000000
2 792 ==> query#resultsetHeader(1,maybeOK,11)
        Resultset header received, expecting 0 column definition packets

stderr

Trace
    at Connection.handlePacket (project\node_modules\mysql2\lib\connection.js:470:15)
    at PacketParser.onPacket (project\node_modules\mysql2\lib\connection.js:94:16)
    at PacketParser.executeStart (project\node_modules\mysql2\lib\packet_parser.js:77:14)
    at Socket.<anonymous> (project\node_modules\mysql2\lib\connection.js:102:29)
    at emitOne (events.js:115:13)
    at Socket.emit (events.js:210:7)
    at addChunk (_stream_readable.js:252:12)
    at readableAddChunk (_stream_readable.js:239:11)
    at Socket.Readable.push (_stream_readable.js:197:10)
    at TCP.onread (net.js:589:20)
Trace
    at Connection.handlePacket (project\node_modules\mysql2\lib\connection.js:470:15)
    at PacketParser.onPacket (project\node_modules\mysql2\lib\connection.js:94:16)
    at PacketParser.executeStart (project\node_modules\mysql2\lib\packet_parser.js:77:14)
    at Socket.<anonymous> (project\node_modules\mysql2\lib\connection.js:102:29)
    at emitOne (events.js:115:13)
    at Socket.emit (events.js:210:7)
    at addChunk (_stream_readable.js:252:12)
    at readableAddChunk (_stream_readable.js:239:11)
    at Socket.Readable.push (_stream_readable.js:197:10)
    at TCP.onread (net.js:589:20)
Trace
    at Connection.handlePacket (project\node_modules\mysql2\lib\connection.js:470:15)
    at PacketParser.onPacket (project\node_modules\mysql2\lib\connection.js:94:16)
    at PacketParser.executeStart (project\node_modules\mysql2\lib\packet_parser.js:77:14)
    at Socket.<anonymous> (project\node_modules\mysql2\lib\connection.js:102:29)
    at emitOne (events.js:115:13)
    at Socket.emit (events.js:210:7)
    at addChunk (_stream_readable.js:252:12)
    at readableAddChunk (_stream_readable.js:239:11)
    at Socket.Readable.push (_stream_readable.js:197:10)
    at TCP.onread (net.js:589:20)

  TypeError: undefined is not a function
      at router.post (project\api\user.js:131:1)
      at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:169:7)
sidorares commented 7 years ago

can you try non-promise version first and see if you get error?

also, can you show code at project\api\user.js:131 ?

zfanta commented 7 years ago

@sidorares project\api\user.js:131 is just the end of the function. 131

I tried non-promise version and it works well. but I'm using koa.js so I prefer to use promise version.

debug log(non-promise)

Add command: Connection
Add command: query
 raw: 0a352e362e3336001c0300003c7d24362248237500fff72102007f8015000000000000000000004537244b2f4a643a33323f29006d7973716c5f6e61746976655f70617373776f726400
1 undefined ==> Connection#handshakeInit(0,,78)
Server hello packet: capability flags:2155870207=(long password, found rows, long flag, connect with db, no schema, compress, odbc, local files, ignore space, protocol 41, interactive, ignore sigpipe, transactions, reserved, secure connection, multi statements, multi results, ps multi results, plugin auth, connect attrs, plugin auth lenenc client data, can handle expired passwords, remember options)
Sending handshake packet: flags:8582095=(long password, found rows, long flag, connect with db, odbc, local files, ignore space, protocol 41, ignore sigpipe, transactions, reserved, secure connection, multi results, session track)
1 796 <== Connection#handshakeInit(1,,75)
1 796 <== 47000001cff3820000000000e00000000000000000000000000000000000000000000000726f6f74001476f6cd7d0dfb10f8997a5fce3dd48f293a206b247563616e2d6170692d6e657700
 raw: 00000002000000
1 796 ==> Connection#handshakeResult(2,maybeOK,11)
        Sending query command: INSERT INTO `user` (`email`, `password`, `name`, `comment`) VALUES ('8123j@asd.com', '$2a$10$qQufIliQbKeha2t9YKxd6.7Flv/eVXHBZhFaLKIJ9eynCBWiRiRkq', 'lkasd', 'nkl') ON DUPLICATE KEY UPDATE `password` = '$2a$10$qQufIliQbKeha2t9YKxd6.7Flv/eVXHBZhFaLKIJ9eynCBWiRiRkq', `name` = 'lkasd', `comment` = 'nkl'
1 796 <== query#start(0,,306)
1 796 <== 2e01000003494e5345525420494e544f20607573657260202860656d61696c602c206070617373776f7264602c20606e616d65602c2060636f6d6d656e7460292056414c554553202827383132336a406173642e636f6d272c20272432612431302471517566496c6951624b656861327439594b7864362e37466c762f65565848425a6846614c4b494a3965796e434257695269526b71272c20276c6b617364272c20276e6b6c2729204f4e204455504c4943415445204b455920555044415445206070617373776f726460203d20272432612431302471517566496c6951624b656861327439594b7864362e37466c762f65565848425a6846614c4b494a3965796e434257695269526b71272c20606e616d6560203d20276c6b617364272c2060636f6d6d656e7460203d20276e6b6c27
 raw: 00022302000000
1 796 ==> query#resultsetHeader(1,maybeOK,11)
        Resultset header received, expecting 0 column definition packets

stderr(non-promise)

Trace
    at Connection.handlePacket (project\node_modules\mysql2\lib\connection.js:470:15)
    at PacketParser.onPacket (project\node_modules\mysql2\lib\connection.js:94:16)
    at PacketParser.executeStart (project\node_modules\mysql2\lib\packet_parser.js:77:14)
    at Socket.<anonymous> (project\node_modules\mysql2\lib\connection.js:102:29)
    at emitOne (events.js:115:13)
    at Socket.emit (events.js:210:7)
    at addChunk (_stream_readable.js:252:12)
    at readableAddChunk (_stream_readable.js:239:11)
    at Socket.Readable.push (_stream_readable.js:197:10)
    at TCP.onread (net.js:589:20)
Trace
    at Connection.handlePacket (project\node_modules\mysql2\lib\connection.js:470:15)
    at PacketParser.onPacket (project\node_modules\mysql2\lib\connection.js:94:16)
    at PacketParser.executeStart (project\node_modules\mysql2\lib\packet_parser.js:77:14)
    at Socket.<anonymous> (project\node_modules\mysql2\lib\connection.js:102:29)
    at emitOne (events.js:115:13)
    at Socket.emit (events.js:210:7)
    at addChunk (_stream_readable.js:252:12)
    at readableAddChunk (_stream_readable.js:239:11)
    at Socket.Readable.push (_stream_readable.js:197:10)
    at TCP.onread (net.js:589:20)
Trace
    at Connection.handlePacket (project\node_modules\mysql2\lib\connection.js:470:15)
    at PacketParser.onPacket (project\node_modules\mysql2\lib\connection.js:94:16)
    at PacketParser.executeStart (project\node_modules\mysql2\lib\packet_parser.js:77:14)
    at Socket.<anonymous> (project\node_modules\mysql2\lib\connection.js:102:29)
    at emitOne (events.js:115:13)
    at Socket.emit (events.js:210:7)
    at addChunk (_stream_readable.js:252:12)
    at readableAddChunk (_stream_readable.js:239:11)
    at Socket.Readable.push (_stream_readable.js:197:10)
    at TCP.onread (net.js:589:20)
sidorares commented 7 years ago

could it be that router.post is undefined for some reason?

zfanta commented 7 years ago

@sidorares, it was problem of assignment, not this module.

line 115: ... await connection.query('INSERT ... returns [Object, undefined], so lvalue [[row]] had to be like [{insertId},]