我正在尝试使用node passport.js和mongoose创建一个新用户,但它显示了一些错误,如下所示
src
我的模型如下所示
{ [ValidationError: User validation failed]
message: 'User validation failed',
name: 'ValidationError',
errors:
{ mobile:
{ [ValidatorError: Path `mobile` is required.]
properties: [Object],
message: 'Path `mobile` is required.',
name: 'ValidatorError',
kind: 'required',
path: 'mobile',
value: undefined } } }
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt-nodejs');
var passportLocalMongoose = require('passport-local-mongoose');
var UserSchema = new Schema({
name: String,
email: {type: String, required: true, select: true},
mobile: {type: String, required: true, select: true},
password: {type: String, required: true, select: true},
active:{ type: 'Boolean', default: false},
generatedOtp:{ type: String},
resetPasswordToken: String,
resetPasswordExpires: Date,
});
UserSchema.pre('save', function (next) {
var user = this;
if(!user.isModified('password')) return next();
bcrypt.hash(user.password, null, null, function(err, hash){
if(err) return next(err);
user.password = hash;
next();
});
});
UserSchema.methods.comparePassword = function(password){
var user = this;
return bcrypt.compareSync(password, user.password);
}
UserSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model('User', UserSchema);
使用此功能,我无法创建新用户。请建议如何解决这个问题。