"Path 'hashed_password' is required" 即使在使用虚拟字段时 hash_password 字段已满
"Path 'hashed_password' is required" even though hash_password field is full when using virtual field
我正在尝试创建一个应用程序,将密码发送到虚拟字段,然后进行哈希处理并存储为哈希值。但是我不断收到此错误:
(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
当我没有加密密码功能时,我仍然得到一个错误:
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' }
{
_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.
尝试使用 function(password)
而不是 password =>
。
当您使用箭头函数时,this
不是指您正在保存的用户,这也是为什么您在控制台登录时看不到姓名和电子邮件的原因。
而不是使用 password => {...}
,声明一个适当的函数可能有助于解决您的问题:function(password)
。
同时检查您的 encryptPassword
函数,错误很可能来自那里。
示例:
userSchema.methods = {
// below function will get the plain password
encryptPassword: function (password) {
if (!password)
return "";
return crypto.createHmac('sha1', this.salt)
.update(password)
.digest('hex');
}
}
我正在尝试创建一个应用程序,将密码发送到虚拟字段,然后进行哈希处理并存储为哈希值。但是我不断收到此错误:
(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
当我没有加密密码功能时,我仍然得到一个错误:
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' }
{
_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.
尝试使用 function(password)
而不是 password =>
。
当您使用箭头函数时,this
不是指您正在保存的用户,这也是为什么您在控制台登录时看不到姓名和电子邮件的原因。
而不是使用 password => {...}
,声明一个适当的函数可能有助于解决您的问题:function(password)
。
同时检查您的 encryptPassword
函数,错误很可能来自那里。
示例:
userSchema.methods = {
// below function will get the plain password
encryptPassword: function (password) {
if (!password)
return "";
return crypto.createHmac('sha1', this.salt)
.update(password)
.digest('hex');
}
}