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
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)
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.There is no
err.message
somatch
isnull
, and there are 2 statements that don’t guard against it.and
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,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 😃