2017-05-13 151 views
0

我收到以下錯誤,當我試圖打電話給我在我的用戶模型中定義的generateHash實例方法:無法訪問Sequelize實例方法

User.generateHash(...).then is not a function

這裏的模型定義本身:

const User = sequelize.define('User', 
{ 
    firstName: { 
     type: Sequelize.TEXT, 
     field: 'first_name' 
    }, 
    lastName: { 
     type: Sequelize.TEXT, 
     allowNull: false, 
     field: 'last_name' 
    }, 
    userName: { 
     type: Sequelize.TEXT, 
     field: 'user_name', 
     allowNull: false 
    }, 
    password: { 
     type: Sequelize.TEXT, 
     allowNull: false 
    } 
}, { 
    tableName: 'users', 
    underscored: true, 
    classMethods: { 
     associate: function(models) { 
      User.hasMany(
     models.Trip, 
       { 
        as: 'trips', 
        foreignKey: { 
         name: 'userId', 
         field: 'user_id', 
         allowNull: false 
        }, 
        onDelete: 'CASCADE' 
       }); 
     }, 
    }, 
    instanceMethods: { 
     generateHash: function(password) { 
      return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null); 
     }, 
     validatePassword: function(password) { 
      return bcrypt.compareSync(password, this.password); 
     }, 
     apiRepr: function() { 
      return { 
       id: this.id, 
       firstName: this.firstName, 
       lastName: this.lastName, 
       userName: this.userName 
      }; 
     } 
    } 
}); 

這裏就是我試圖調用該方法端點:

router.post('/', (req, res) => { 
let {userName, password, firstName, lastName} = req.body; 

// if no existing user, hash password 
return User.generateHash(password) 
    .then(hash => { 
     // create new user 
     return User.create({ 
      firstName: firstName, 
      lastName: lastName, 
      userName: userName, 
      password: hash 
     }); 
    }) 
    .then(user => { 
     // send back apirRepr data 
     return res.status(201).json(user.apiRepr()); 
    }) 
    // error handling 
    .catch(err => { 
     if (err.name === 'AuthenticationError') { 
      return res.status(422).json({message: err.message}); 
     } 
     res.status(500).json({message: 'Internal server error'}); 
    });}); 

我完全被卡住了。有任何想法嗎?

回答

1

你打電話給.then()的東西,不返回承諾。試試這個:

router.post('/', (req, res) => { 
let {userName, password, firstName, lastName} = req.body; 
let hash = User.generateHash(password); 
// if no existing user, hash password 
return User.create({ 
     firstName: firstName, 
     lastName: lastName, 
     userName: userName, 
     password: hash 
    }) 
    .then(user => { 
     // send back apirRepr data 
     return res.status(201).json(user.apiRepr()); 
    }) 
    // error handling 
    .catch(err => { 
     if (err.name === 'AuthenticationError') { 
      return res.status(422).json({message: err.message}); 
     } 
     return res.status(500).json({message: 'Internal server error'}); 
    }); 
+0

謝謝!我結束了實施類似的解決方案。 –

1

在繼續V4類和實例方法被刪除。 現在你必須做出這樣說:

const Model = sequelize.define('Model', { 
 
    ... 
 
}); 
 

 
// Class Method 
 
Model.associate = function (models) { 
 
    ...associate the models 
 
}; 
 

 
// Instance Method 
 
Model.prototype.someMethod = function() {..}

更多的信息在這裏Sequelize v4 breaking changes