Node.js &引用;路径';哈希密码';“是必需的”;即使使用虚拟字段时哈希_密码字段已满
我正在尝试创建一个应用程序,其中密码被发送到一个虚拟字段,然后散列并存储为散列。然而,我不断地得到这个错误:Node.js &引用;路径';哈希密码';“是必需的”;即使使用虚拟字段时哈希_密码字段已满,node.js,mongodb,mongoose,validationerror,Node.js,Mongodb,Mongoose,Validationerror,我正在尝试创建一个应用程序,其中密码被发送到一个虚拟字段,然后散列并存储为散列。然而,我不断地得到这个错误: (node:32101) UnhandledPromiseRejectionWarning: ValidationError: User validation failed: hashed_password: Path `hashed_password` is required. 下面是我的代码,当我运行它时,我会得到代码下面包含的日志 const mongoose = require
(node:32101) UnhandledPromiseRejectionWarning: ValidationError: User validation failed: hashed_password: Path `hashed_password` is required.
下面是我的代码,当我运行它时,我会得到代码下面包含的日志
const mongoose = require('mongoose');
const uuidv1 = require('uuid/v1');
const cryptop = require('crypto');
const userSchema = new mongoose.Schema({
name: {
type: String,
trim: true,
required: true
},
email: {
type: String,
trim: true,
required: true
},
hashed_password: {
type: String,
required: true
},
salt: String,
created: {
type: Date,
default: Date.now
},
updated: Date
});
userSchema
.virtual("password")
.set(password => {
// create temporary variable called _password
this._password = password;
// generate a timestamp
this.salt = uuidv1();
// encryptPassword()
this.hashed_password = this.encryptPassword(password);
console.log(this);
})
.get(function () {
return this._password;
});
userSchema.methods = {
encryptPassword: password => {
if (!password) return "";
try {
return crypto
.createHmac("sha1", this.salt)
.update(password)
.digest("hex");
} catch (err) {
return "";
}
}
};
module.exports = mongoose.model("User", userSchema);
错误:
Express is listening on port 8080
DB connected
{ name: 'Ryan', email: 'ryan1@gmail.com', password: 'rrrrr' }
(node:32477) UnhandledPromiseRejectionWarning: TypeError: this.encryptPassword is not a function
Express is listening on port 8080
DB connected
{ name: 'Ryan', email: 'ryan1@gmail.com', password: 'rrrrr' }
{
_password: 'rrrrr',
salt: 'ff790ca0-34f0-11ea-9394-a53427d4f6bb',
hashed_password: 'Test hash'
}
(node:32577) UnhandledPromiseRejectionWarning: ValidationError: User validation failed: hashed_password: Path `hashed_password` is required.
当我在没有encryptPassword功能的情况下执行此操作时,仍然会出现一个错误:
const mongoose = require('mongoose');
const uuidv1 = require('uuid/v1');
const cryptop = require('crypto');
const userSchema = new mongoose.Schema({
name: {
type: String,
trim: true,
required: true
},
email: {
type: String,
trim: true,
required: true
},
hashed_password: {
type: String,
required: true
},
salt: String,
created: {
type: Date,
default: Date.now
},
updated: Date
});
userSchema
.virtual("password")
.set(password => {
// create temporary variable called _password
this._password = password;
// generate a timestamp
this.salt = uuidv1();
// encryptPassword()
// this.hashed_password = this.encryptPassword(password);
this.hashed_password = 'Test hash';
console.log(this);
})
.get(function () {
return this._password;
});
userSchema.methods = {
encryptPassword: password => {
if (!password) return "";
try {
return crypto
.createHmac("sha1", this.salt)
.update(password)
.digest("hex");
} catch (err) {
return "";
}
}
};
module.exports = mongoose.model("User", userSchema);
错误:
Express is listening on port 8080
DB connected
{ name: 'Ryan', email: 'ryan1@gmail.com', password: 'rrrrr' }
(node:32477) UnhandledPromiseRejectionWarning: TypeError: this.encryptPassword is not a function
Express is listening on port 8080
DB connected
{ name: 'Ryan', email: 'ryan1@gmail.com', password: 'rrrrr' }
{
_password: 'rrrrr',
salt: 'ff790ca0-34f0-11ea-9394-a53427d4f6bb',
hashed_password: 'Test hash'
}
(node:32577) UnhandledPromiseRejectionWarning: ValidationError: User validation failed: hashed_password: Path `hashed_password` is required.
尝试使用函数(密码)
而不是密码=>
当您使用箭头功能时,此
不是指您正在保存的用户,这也是为什么您在控制台登录时看不到姓名和电子邮件的原因