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.

TypeError: Cannot read property '2' of null

See original GitHub issue

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

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
akhourycommented, Apr 21, 2018

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

1reaction
akhourycommented, Apr 20, 2018

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)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncaught TypeError: Cannot read property '2' of null
Uncaught TypeError: Cannot read property '2' of null. The error appears only when reloading the page. So I assume the cookie is there...
Read more >
Uncaught TypeError: Cannot read property of null - iDiallo
This error occurs when you read a property or call a method on a null object . That's because the DOM API returns...
Read more >
TypeError: Cannot read property 'value' of Null in JS
To solve the "Cannot read property 'value' of null" error, make sure that the JS script tag is placed at the bottom of...
Read more >
Uncaught (in promise) TypeError: Cannot read property '2' of null
Any help would be appreciated. The error is “Uncaught (in promise) TypeError: Cannot read property '2' of null” in www/framework7/js/framework7.
Read more >
[Charts] - Reactive : Cannot read property '2' of null.
[Charts] - Reactive : Cannot read property '2' of null. Question. Charts. Hi Outsys Devs! I'm asking for help on how to solve...
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