我正在尝试将Sequelize V3升级到V4。
Followed the breaking changes instructions here.
使用V4运行以下查询时出现错误:
Auth.findOne({ . // This is where it failes
include: {
required: true,
include: {
where: {
id: decoded.id,
token: token,
}
}
}
})
设置:
V3中的现有代码(有效):
module.exports = function (sequelize, DataTypes) {
const Auth = sequelize.define('Auth', {
token: {
type: DataTypes.STRING,
allowedNull: true,
unique: true,
required: true,
validate: {
len: [5, 200]
}
}
device: {
type: DataTypes.STRING,
allowedNull: true,
unique: false,
}
}, {
tableName: 'Auth',
classMethods: {
associate: function (models) {
Auth.belongsTo(models.User, {
foreignKey: {
name: 'user_id',
allowedNull: false,
}
})
Auth.hasMany(models.Endpoint, {
as: 'endpoints',
foreignKey: 'auth_id'
})
},
findByToken: function (token) {
var User = this
var decoded
try {
decoded = jwt.verify(token, 'abc123')
} catch (e) {
return Promise.reject()
}
return Auth.findOne({
where: {
id: decoded.id,
token: token,
}
})
}
}, instanceMethods: {
generateAuthToken: function () {
var auth = this
var access = 'auth'
var token = jwt.sign({ id: auth.id, access }, 'abc123').toString()
auth.token = token
auth.code = null
auth.save().then(() => {
})
}
}
})
return Auth
}
升级到V4(发生TypeError)
module.exports = function (sequelize, DataTypes) {
var Auth = sequelize.define('Auth', {
token: {
type: DataTypes.STRING,
allowedNull: true,
unique: true,
required: true,
validate: {
len: [5, 200]
}
},
device: {
type: DataTypes.STRING,
allowedNull: true,
unique: false,
}
})
Auth.associate = function (models) {
Auth.belongsTo(models.User, {
foreignKey: {
name: 'user_id',
allowedNull: false,
}
})
Auth.hasMany(models.Endpoint, {
as: 'endpoints',
foreignKey: 'auth_id'
})
}
Auth.findByToken = function (token) {
var User = this
var decoded
try {
decoded = jwt.verify(token, 'abc123')
} catch (e) {
return Promise.reject()
}
return Auth.findOne({ . // This is where it fails
include: {
required: true,
include: {
where: {
id: decoded.id,
token: token,
}
}
}
})
}
Auth.prototype.generateAuthToken = function () {
var auth = this
var access = 'auth'
var token = jwt.sign({ id: auth.id, access }, 'abc123').toString()
auth.token = token
auth.code = null
auth.save().then(() => {
})
}
return Auth
}
中间件
var authenticate = (req, res, next) => {
var token = req.header('x-auth')
var db = req.app.get('db')
db.Auth.findByToken(token).then((auth) => {
if (!auth) {
return Promise.reject()
}
req.user_id = auth.user_id
req.device_id = auth.device_id
req.auth_id = auth.id
return next()
}).catch((e) => {
return res.status(401).send()
// I get an error here: `TypeError: Cannot read property '_expandIncludeAll' of undefined
})
}
我在做什么错了?
答案 0 :(得分:0)
您是否尝试过不使用“包含”?
类似的东西:
return Auth.findOne({ . // This is where it fails
where: {
id: decoded.id,
token: token,
}
});
应该工作。
“包含”用于通过关联进行子查询,但是您的where子句似乎仅在Auth属性(http://docs.sequelizejs.com/class/lib/model.js~Model.html#static-method-findAll)上
如果要过滤User的某些属性,请使用“ include”。
答案 1 :(得分:0)
您必须做include : { model : YourModel }
。
执行include: { include : Model}
会引发此确切错误。