Sequelize OP -> TypeError: Cannot read property 'like' of undefined

Viewed 963

In my project, i'd like to search many subways have a certain keyword.

So, I use Op in Sequelize, but it returned TypeError: Cannot read property 'like' of undefined.

The way I initialized the sequelize object is as follows.

// root/models/index.js

const Sequelize = require('sequelize');

const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const db = {};

const sequelize = new Sequelize(config.database, config.username, config.password, config);

db.sequelize = sequelize;

module.exports = db;

And, I initialized and used the Op object like this

const { Subway } = require("../../models");
const sequelize = require("../../models").sequelize;
const Op = sequelize.Op;

const list = await Subway.findAll({
            where: {
                station_name_kr: {
                    [Op.like]: '%' + req.params.filter + '%',
                },
            },
            attributes: [
                'idx',
                'station_line',
                'station_name_kr'
            ],
        });

But it didn't work....

Could you tell me what is the problem?


Edit 1

// models/subway.js
const Sequelize = require('sequelize');

module.exports = class Subway extends Sequelize.Model {
    static init(sequelize){
        return super.init({
            idx: {
                type: Sequelize.INTEGER,
                primaryKey: true,
                autoIncrement: true
            },
            station_code: {
                type: Sequelize.CHAR(20),
            },
            station_name_kr: {
                type: Sequelize.CHAR(20),
                allowNull: false,
            },
            station_name_en: {
                type: Sequelize.CHAR(30),
            },
            station_line: {
                type: Sequelize.CHAR(10),
            },
            outside_code: {
                type: Sequelize.CHAR(10),
            },
        }, {
            sequelize,
            timestamps: true,
            underscored: true,
            paranoid: true,
            modelName: 'subway',
            tableName: 'Subway',
            charset: 'utf8',
            collate: 'utf8_general_ci',
        });
    }
};

2 Answers

The problem in your code is that you are mistaking two things.

First one: Sequelize object from requiring library.

Second one: sequelize connection from "instantiating" the Sequelize Object.

Note the upper case and lower case (s)(S)equelize because they represent two distinct objects.

Below what you are trying to do is access the connection and from there the Operator (Op) property. It is not going to work. The sequelize connection does NOT have it.

const sequelize = require("../../models").sequelize; 
const Op = sequelize.Op;

To be able to access you should do the following:

const Sequelize = require('sequelize');
const Op = sequelize.Op;

That is why your answer "works". But that answer is not why it works.

Oh the answer was very simple...

I just initialize a Op object like this.

const Op = require("sequelize").Op;

Then, I could use the Op object with like, or , and etc...

Related