I am getting an error ValidationError: Path password is required, when trying to register a new User. I am new to Node, programming, and authentication.
Here is the User model and schema code I have, I attempted to comment it thoroughly.
User router
router.post("/register", async (req, res) => { try { let {email, password, passwordCheck, displayName} = req.body;
// validate email, password and passwordCheck if (!email || !password || !passwordCheck) return res.status(400).json({ msg: "Not all fields have been entered." }); if (password.length < 5) return res.status(400).json({ msg: "The password needs to be at least 5 characters long." }); if (password !== passwordCheck) return res.status(400).json({ msg: "Passwords do not match. Enter the same password twice for verification." }); //verify if the new user already exists in the MongoDB const existingUser = await User.findOne({ email: email }); if (existingUser) return res.status(400).json({ msg: "An account with this email already exists." }); if (!displayName) displayName = email; //if no display name, email is display name const salt = await bcrypt.genSalt(10); //creates random string used for hashing password const passwordHash = await bcrypt.hash(password, salt, function (err, hash) { if (err) { console.log('Issue with hashing bcrypt'); }// else {console.log('Successful Hash: ' + hash);} add if error checking }); //save new user to DB with hashed password const newUser = new User({ email, "password":passwordHash, displayName, }); console.log(newUser); const savedUser = await newUser.save(); //save the new user to database res.json(savedUser); //respond to front end with saved user } catch (err) { console.log('Error registering user'); res.status(500).json({ err });} });
User Model
const userSchema = new mongoose.Schema({ email: {type: String, required: true, unique: true}, password: {type: String, required: true, minlength: 5}, displayName: {type: String} });