代码之家  ›  专栏  ›  技术社区  ›  user1107173

sequentize:TypeError:无法读取未定义的属性'\u expandIncludeAll'

  •  0
  • user1107173  · 技术社区  · 6 年前

    我正在尝试将SequelizeV3升级到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(works)的现有代码:

    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
    

    }

    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
        })
    }
    

    我做错什么了?

    2 回复  |  直到 6 年前
        1
  •  0
  •   Cley Faye    6 年前

    你试过没有“包括”这个词吗?

    类似于:

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

    应该有用。

    http://docs.sequelizejs.com/class/lib/model.js~Model.html#static-method-findAll )

    如果您想过滤用户的某些属性,可以使用“include”。

        2
  •  0
  •   Mário    6 年前

    你必须这么做 include : { model : YourModel }

    include: { include : Model}