Javascript 实例方法don';我不在续集4上工作
型号代码如下:Javascript 实例方法don';我不在续集4上工作,javascript,node.js,sequelize.js,Javascript,Node.js,Sequelize.js,型号代码如下: 'use strict'; const bcrypt = require('bcrypt'); module.exports = (sequelize, DataTypes) => { const User = sequelize.define('User', { email: { type: DataTypes.STRING, allowNull: false, unique: true, }, passw
'use strict';
const bcrypt = require('bcrypt');
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', {
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
password: {
type: DataTypes.STRING,
allowNull: false,
},
}, {
hooks: {
beforeCreate: user => {
const salt = bcrypt.genSaltSync();
user.password = bcrypt.hashSync(user.password, salt);
}
},
});
User.prototype.isPasswordValid = password => {
console.log('current_email');
console.log(this.email);
//return bcrypt.compareSync(password, this.password);
};
User.associate = models => {
// associations can be defined here
};
return User;
};
当我执行此代码时:
const user = await User.findOne({ where: { email } });
if (!user || !user.isPasswordValid(password)) {
ctx.body = {
result: RESULT_CODE.ERROR,
error: ERROR_CODE.UNAUTHORIZED,
};
return;
}
我看到以下输出:
current_email
undefined
我不明白为什么我不能访问用户字段
版本:
"sequelize": "4.3.1",
"sequelize-cli": "4.0.0"
尝试使用常用函数,而不是箭头函数
User.prototype.isPasswordValid = function(password) {
console.log('current_email');
console.log(this.email);
//return bcrypt.compareSync(password, this.password);
};