0

Getting validation error when trying to sign up new user to database. Not sure where to go from here

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);

Error : ValidationError: User validation failed: password: cannot be empty, email: cannot be empty, lastName: cannot be empty, firstName: cannot be empty    at model.Document.invalidate (C:\Users\Dominick\Desktop\4166\Dominick Project 3\node_modules\mongoose\lib\document.js:2696:32)    at C:\Users\Dominick\Desktop\4166\Dominick Project 3\node_modules\mongoose\lib\document.js:2516:17    at C:\Users\Dominick\Desktop\4166\Dominick Project 3\node_modules\mongoose\lib\schematype.js:1238:9    at processTicksAndRejections (internal/process/task_queues.js:77:11)

1 Answer 1

1

You need datas !

In your code you can't put empty fields, so just running this code in NodeJS won't work. try to create an object containing the datas.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.