sequelize:typeerror:无法读取未定义的属性“\u expandincludeall”

58wvjzkj  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(399)

我正在尝试将SequelizeV3升级到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
    })
}

我做错什么了?

2lpgd968

2lpgd9681#

你试过没有“包含”吗?
比如:

return Auth.findOne({ . // This is where it fails
    where: {
        id: decoded.id,
        token: token,
    }
});

应该有用。
“include”用于通过关联进行的子查询,但是where子句似乎只在auth属性上(http://docs.sequelizejs.com/class/lib/model.js~model.html#static-方法(芬德尔)
如果你想过滤用户的某些属性,你可以使用include。

irtuqstp

irtuqstp2#

你必须这么做 include : { model : YourModel } .
include: { include : Model} 抛出这个错误。

相关问题