node-mysql2 icon indicating copy to clipboard operation
node-mysql2 copied to clipboard

Packet.readDateTime cannot read length of undefined

Open Mattchewone opened this issue 5 years ago • 4 comments

Upgrading from 1.6.1 to 1.7.0 and I am hitting this error:

node_modules/mysql2/lib/packets/packet.js:286
    if (str.length === 10) {
            ^

TypeError: Cannot read property 'length' of undefined
    at Packet.readDateTime (/user/node_modules/mysql2/lib/packets/packet.js:286:13)
    at new BinaryRow (eval at line.toFunction (/user/node_modules/generate-function/index.js:172:21), <anonymous>:2431:41)
    at Execute.row (/user/node_modules/mysql2/lib/commands/query.js:230:17)
    at Execute.execute (/user/node_modules/mysql2/lib/commands/command.js:39:22)
    at Connection.handlePacket (/user/node_modules/mysql2/lib/connection.js:408:32)
    at PacketParser.Connection.packetParser.p [as onPacket] (/user/node_modules/mysql2/lib/connection.js:70:12)
    at PacketParser.executeStart (/user/node_modules/mysql2/lib/packet_parser.js:75:16)
    at PacketParser.executePayload (/user/node_modules/mysql2/lib/packet_parser.js:167:21)
    at Socket.Connection.stream.on.data (/user/node_modules/mysql2/lib/connection.js:77:25)
    at Socket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at Socket.Readable.push (_stream_readable.js:224:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)

Mattchewone avatar Sep 27 '19 12:09 Mattchewone

hi @Mattchewone can you reduce this to simple self contained example so I can run it on my side? Ideally include schema creation and initial data

sidorares avatar Oct 27 '19 10:10 sidorares

I've replicated this error with table having default value for date or datetime columns e.g.

CREATE TABLE `user` (
  `userid` int(11) NOT NULL AUTO_INCREMENT,  
  `dob` date DEFAULT '0000-00-00'
  PRIMARY KEY (`userid`)
) ENGINE=InnoDB;

and when one or more records had default value set. This error was happening only when I was fetching records with limit applied. Work around was to pass 'dateStrings: true' in 'dialectOptions' property when creating connection to MySQL.

grzegorziskierka avatar Nov 12 '19 10:11 grzegorziskierka

No meu caso o problema foi resolvido quando no banco de dados datetime erra null ou quando a data era diferente de 0000-00-00 00:00:00

b2evandro avatar Dec 23 '20 17:12 b2evandro

This error is still present on 3.6.3.

We have a column defined as created_at datetime(3) NOT NULL DEFAULT current_timestamp(3)

We are using AWS Aurora 2 (MySQL 5.7). (No SQL Strict mode enabled)

Our connections options include timezone: '+00:00' and dateStrings: ['DATE']

Somebody decided put the value 0000-00-00 00:00:00.000 into this field.

readDateTime() just jumps past all the checks at the top https://github.com/sidorares/node-mysql2/blob/master/lib/packets/packet.js#L257 because of our timezone setting and ends up just calling readDateTimeString() https://github.com/sidorares/node-mysql2/blob/master/lib/packets/packet.js#L295

The length read at the top of readDateTimeString() https://github.com/sidorares/node-mysql2/blob/master/lib/packets/packet.js#L303 is 0 (I added a console.log here to verify) and str is never initialized at https://github.com/sidorares/node-mysql2/blob/master/lib/packets/packet.js#L311 so this function returns undefined.

Back in readDateTime() it tries to read .length of undefined at https://github.com/sidorares/node-mysql2/blob/master/lib/packets/packet.js#L296 which throws an error.

As a work-around, we just changed our timezone setting to 'Z', which is functionally the same as '+00:00' but allows the special check in readDateTime() to create the Date object successfully, since every variable is initialized to zero here.

As a fix (in case people wanted some non-zero timezone offset), str should just be initialized to an empty string inside readDateTimeString() at https://github.com/sidorares/node-mysql2/blob/master/lib/packets/packet.js#L311

ryanrubleycoates avatar Nov 06 '23 22:11 ryanrubleycoates