node.js Sequelize.op 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46580397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sequelize.op is not defined
提问by Ana?s Saez
I'm using sequelize with Node.js. I'm trying to use Sequelize.op request. But it doesn't work, this is my code :
我在 Node.js 中使用 sequelize。我正在尝试使用 Sequelize.op 请求。但它不起作用,这是我的代码:
var Sequelize = require('sequelize');
const Op = Sequelize.Op;
const operatorsAliases = {
$eq: Op.eq
}
This is the error in the node console :
这是节点控制台中的错误:
Do you have any idea ?
你有什么主意吗 ?
Thank you
谢谢
回答by Ana?s Saez
I found the solution, I updated the sequelize version via npm, and it's work. So the solution is
我找到了解决方案,我通过 npm 更新了 sequelize 版本,它的工作。所以解决办法是
npm i [email protected] --s
npm i [email protected] --s
回答by alditis
Last version for now:
目前最新版本:
4.22.2
models/user.js:
模型/user.js:
const Sequelize = require('sequelize');
const op = Sequelize.Op;
const operatorsAliases = {
$eq: op.eq,
$or: op.or,
}
module.exports = function(sequelize) {
var User = sequelize.define('user', {
id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
email: { type: Sequelize.STRING },
username: { type: Sequelize.STRING(120) },
...
});
User.beforeCreate((user, options) => {
var where = {
type: 1,
deleted: null,
// With aliases
$or: [{email: {$eq: user.email} }, { username: {$eq: user.username}}]
// Without aliases: Last version
//[op.or]: [ { email: user.email }, { username: user.username } ]
};
return User.findOne({ where: where })
.then(userFound => {
...
})
.catch(err => {
...
});
});
...
return User;
};
回答by The Red Pea
I had to downgrade
我不得不降级
My package.json specified "sequelize":"latest"
我的 package.json 指定 "sequelize":"latest"
So my server installed sequelize "version": "5.8.2"
所以我的服务器安装了 sequelize "version": "5.8.2"
Apparently this version of Sequelize does nothave Sequelize.Op; so you will see the error.
显然,这个版本Sequelize的并不会有Sequelize.Op; 所以你会看到错误。
So I downgraded back to version 4.41.2; I specified explicitly in my package.json "sequelize":"4.41.2"
所以我降级回版本4.41.2;我在 package.json 中明确指定"sequelize":"4.41.2"
回答by Janier Hernandez
I solved (upgrading from v 4.44.x to v 5.21.4) adding
const { Op } = require("sequelize");
我解决了(从 v 4.44.x 升级到 v 5.21.4)添加
const { Op } = require("sequelize");


