将用户添加到 MongoDB 数据库时出错



尝试将新用户注册到数据库时出现验证错误。不知道从这里去哪里

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const bcrypt = require('bcrypt');
const userSchema = new Schema({
firstName: {type: String, required: [true, 'cannot be empty']},
lastName: {type: String, required: [true, 'cannot be empty']},
email: { type: String, required: [true, 'cannot be empty'], unique: [true, 'this email address has been used'] },
password: { type: String, required: [true, 'cannot be empty'] },
}
);
//replace plaintext password
//pre middleware
userSchema.pre('save', function(next){
let user = this;
if (!user.isModified('password'))
return next();
bcrypt.hash(user.password, 10)
.then(hash => {
user.password = hash;
next();
})
.catch(err => next(error));
});
//comparison of password and hash
userSchema.methods.comparePassword = function(loginPassword) {
return bcrypt.compare(loginPassword, this.password);
}
module.exports = mongoose.model('User', userSchema);

错误: 验证错误:用户验证失败:密码:不能为空,电子邮件:不能为空,姓氏:不能为空,名字:不能为空    在模型。Document.invalidate (C:\Users\Dominick\Desktop\4166\Dominick Project 3ode_modules\mongoose\lib\document.js:2696:32)    at C:\Users\Dominick\Desktop\4166\Dominick Project 3ode_modules\mongoose\lib\document.js:2516:17    at C:\Users\Dominick\Desktop\4166\Dominick Project 3ode_modules\mongoose\lib\schematype.js:1238:9    at processTicksAndRejections (internal/process/task_queues.js:77:11)

你需要数据!

在您的代码中,您不能放置空字段,因此仅在 NodeJS 中运行此代码是行不通的。 尝试创建一个包含数据的对象。

最新更新