I am trying to save password in MongoDB but have error:
Error: User validation failed: password: Path `password` is required.
Model:
const {Schema, model} = require('mongoose');
const UserSchema = new Schema({
email: {type: String, unique: true, required: true},
password: {type: String, required: true},
isActivated: {type: Boolean, default: false},
activationLink: {type: String},
})
module.exports = model('User', UserSchema)
create user:
const bcrypt = require('bcryptjs');
const UserModel = require('../models/user-model');
...
const hashPassword = await bcrypt.hash(password, 10);
console.log(`email => ${email} pass => ${hashPassword}`);
const user = await UserModel.create({email, hashPassword});
Log shows that password hash created:
email => [email protected] pass => $2a$10$4IV2Q0ZncWHInfT89Fwl.eCxCgvykvY.uqlvq0GNSeDJ/6Q83T7nK
2
Answers
If you pass
hashPassword
variable, it will take ashashPassword
field.Change your code like this: