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',
});
}
};