猫鼬模式上的未定义字段
undefined field on mongoose Schema
我在尝试比较用户密码时遇到未定义的字段
这是函数
logUser = (req,res) => {
User.findOne({ username:req.body.username}, (err,user) => {
if(!user){
req.flash('error','El nombre de usuario no existe');
res.redirect('/login');
}else if(!user.comparePassword(req.body.password)){
req.flash('error','La contraseña no es correcta');
res.redirect('/login');
}else{
res.redirect('/');
}
});
}
当执行转到 else if 并使用 comparePassword
函数时,架构字段为 undefined
架构文件
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const bcrypt = require('bcrypt');
const userSchema = Schema({
username:String,
email:String,
password:String,
registrationDate: {type:Date, defaul: Date.now()}
});
userSchema.methods.encryptPassword = (password) =>{
return bcrypt.hashSync(password, bcrypt.genSaltSync(10));
}
userSchema.methods.comparePassword = (password) =>{
console.log('password: ',this.password);
return bcrypt.compareSync(password, this.password);//here this.password is undefined
}
module.exports = mongoose.model('User',userSchema)
调试我看到 user
对象不是 schem class,tuis class 有什么问题?
简而言之,你替换就可以了
userSchema.methods.comparePassword = (password) =>{
console.log('password: ',this.password);
return bcrypt.compareSync(password, this.password);//here this.password is undefined
}
和
userSchema.methods.comparePassword = function(password) {
console.log('password: ',this.password);
return bcrypt.compareSync(password, this.password);//here this.password is undefined
}
您遇到问题是因为 arrow function
的 lexical scoping
。箭头函数从父级继承其 this
作用域。
我在尝试比较用户密码时遇到未定义的字段
这是函数
logUser = (req,res) => {
User.findOne({ username:req.body.username}, (err,user) => {
if(!user){
req.flash('error','El nombre de usuario no existe');
res.redirect('/login');
}else if(!user.comparePassword(req.body.password)){
req.flash('error','La contraseña no es correcta');
res.redirect('/login');
}else{
res.redirect('/');
}
});
}
当执行转到 else if 并使用 comparePassword
函数时,架构字段为 undefined
架构文件
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const bcrypt = require('bcrypt');
const userSchema = Schema({
username:String,
email:String,
password:String,
registrationDate: {type:Date, defaul: Date.now()}
});
userSchema.methods.encryptPassword = (password) =>{
return bcrypt.hashSync(password, bcrypt.genSaltSync(10));
}
userSchema.methods.comparePassword = (password) =>{
console.log('password: ',this.password);
return bcrypt.compareSync(password, this.password);//here this.password is undefined
}
module.exports = mongoose.model('User',userSchema)
调试我看到 user
对象不是 schem class,tuis class 有什么问题?
简而言之,你替换就可以了
userSchema.methods.comparePassword = (password) =>{
console.log('password: ',this.password);
return bcrypt.compareSync(password, this.password);//here this.password is undefined
}
和
userSchema.methods.comparePassword = function(password) {
console.log('password: ',this.password);
return bcrypt.compareSync(password, this.password);//here this.password is undefined
}
您遇到问题是因为 arrow function
的 lexical scoping
。箭头函数从父级继承其 this
作用域。