javascript - 续集:TypeError:无法读取未定义的属性“_expandIncludeAll”
问题描述
我正在尝试将 Sequelize V3 升级到 V4。
使用 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
})
}
我究竟做错了什么?
解决方案
Have you tried without the "include" ?
Something like:
return Auth.findOne({ . // This is where it fails
where: {
id: decoded.id,
token: token,
}
});
should work.
"include" is used for subqueries through associations but your where clause seems to be only on the Auth attributes (http://docs.sequelizejs.com/class/lib/model.js~Model.html#static-method-findAll)
You'd use "include" if you wanted to filter on some attributes on User.
推荐阅读
- c# - 如何为另一个类的测试参数设置 IEqualityComparer?
- html - overscroll-behavior 属性不适用于 webview
- docker - 节点 API 失败并出现错误:EHOSTUNREACH
- assembly - S1.geti、S2.geti 都调用同一个过程(称为 Sc_geti)。S1.i 不可用。奇怪的字符
- javascript - 在 parseFloat() 之后将对象键数组中的所有值相加
- javascript - 无法选中第二个循环内的复选框
- python - 使用fuzzywuzzy合并数据框
- solr - Solr MoreLikeThis 处理程序返回 0 个元素
- javascript - Apollo Query 组件如何使用它的自身值作为回调
- javascript - 在状态中设置空对象以生成行数