sequelize: TypeError: Cannot read property '2' of null

What you are doing?

Creating a record with the same value in a column that is Unique:true. I’m using FeathersJS with uses Sequelize. I successfully inserted new record via restful api using Postman. However if I send the same request again( create a user with the same email which the column is unique) it crashes the server with error “TypeError: Cannot read property ‘2’ of null”.

Below is the model I am using

// code here
const Sequelize = require('sequelize');

module.exports = function(sequelize) {
  const user = sequelize.define('users', {
    email: {
      type: Sequelize.STRING,
      allowNull: false,
      unique: true
    },
    password: {
      type: Sequelize.STRING,
      allowNull: false
    }
  }, {
    freezeTableName: true
  });

What do you expect to happen?

I expected to get a response back with an error stating its a duplicate or at least a clean error.

What is actually happening?

Crashed server since error wasn’t caught.

Output, either JSON or SQL

image

Dialect: mysql Database version: 5.7.13 Sequelize version: 3.24.4

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 15 (8 by maintainers)

Most upvoted comments

The issue is here:

https://github.com/sequelize/sequelize/blob/f8a98a145443c890be9cab4c941abda28845e4c0/lib/dialects/mysql/query.js#L199-L212

I put a log statement to see the err and it looks like this.

{
  "code": "ER_DUP_ENTRY",
  "errno": 1062,
  "sqlState": "23000",
  "sqlMessage": "",
  "sql": "INSERT INTO `users` (`id`,`email`,`password`,`createdAt`,`updatedAt`) VALUES (DEFAULT,'xxxxx@gmail.com','$2a$10$0yoLkp7LQD.IcHFfNCib..hL8LeM/soCUdSuOF1c1.NGHjeF8nNbO','2018-04-21 08:52:42','2018-04-21 08:52:42');"
}

There is no err.message so match is null, and there are 2 statements that don’t guard against it.

        const uniqueKey = this.model && this.model.uniqueKeys[match[2]];

and

          fields[match[2]] = match[1];

I am using MySQL 5.7.21

this is till happening when I try to update a record with a column (that is set to unique:true) with a non unique value,

sequelize v4.28.6
node v9.2.0
mysql 5.7.21
/Users/user/code/backend/node_modules/sequelize/lib/dialects/mysql/query.js:186
        const uniqueKey = this.model && this.model.uniqueKeys[match[2]];
                                                                   ^

TypeError: Cannot read property '2' of null
    at Query.formatError (/Users/user/code/backend/node_modules/sequelize/lib/dialects/mysql/query.js:186:68)
    at Query.connection.query [as onResult] (/Users/user/code/backend/node_modules/sequelize/lib/dialects/mysql/query.js:55:23)
    at Query.Command.execute (/Users/user/code/backend/node_modules/mysql2/lib/commands/command.js:30:12)
    at Connection.handlePacket (/Users/user/code/backend/node_modules/mysql2/lib/connection.js:515:28)
    at PacketParser.onPacket (/Users/user/code/backend/node_modules/mysql2/lib/connection.js:94:16)
    at PacketParser.executeStart (/Users/user/code/backend/node_modules/mysql2/lib/packet_parser.js:77:14)
    at Socket.<anonymous> (/Users/user/code/backend/node_modules/mysql2/lib/connection.js:102:29)
    at Socket.emit (events.js:159:13)
    at addChunk (_stream_readable.js:265:12)
    at readableAddChunk (_stream_readable.js:252:11)
    at Socket.Readable.push (_stream_readable.js:209:10)
    at TCP.onread (net.js:598:20)

I’m actually pretty sure the mysql version is the issue then - We’ve had these kind of problems in the past where the error message as different between different versions of mysql.

I think moving forward we should abstract the code that parses messages into a separate method that we can then easily unit test. There is no need to actually run tests against several different versions, but we’d like to be able to support them and at least not crash still 😃