skip to Main Content

I am trying to save a user to mongodb database using post request as follow, but I got the error :
"Error: User validation failed: mail: Path `mail` is required., pseudo: Path `pseudo` is required., password: Path `password` is required."

PS : I edited in response to Eol but my question / and the bug are still present.

Details error :

 errors: {
    mail: ValidatorError: Path `mail` is required.
       [...] {
      properties: [Object],
      kind: 'required',
      path: 'mail',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    },

my controller :

exports.signup = (req, res, next) => {
      const user = new User({
        mail: req.body.mail,
        password: req.body.password,
        pseudo: req.body.pseudo,
      });
      user
        .save()
        .then(() => res.statuts(201).json({ message: "User created !" }))
        .catch(err => console.log(err) )
    }

My User Model :

const mongoose = require("mongoose");
const uniqueValidator = require("mongoose-unique-validator"); 

const userSchema = mongoose.Schema({
  pseudo: { type: String, required: true, unique: true },
  mail: { type: String, required: true, unique: true },
  password: { type: String, required: true },
});

userSchema.plugin(uniqueValidator); 

module.exports = mongoose.model("User", userSchema);

My front :

 const handleSubmit = (e) => {
    e.preventDefault()
    const pseudo = e.target[0].value, 
    password = e.target[1].value, 
    mail = e.target[2].value 
    const data = new FormData()
    data.set("pseudo", pseudo)
    data.set("password", password)
    data.set("mail", mail)

    axios.post(url, data)
    .then(res => console.log(res))
    .catch(res => console.log(res))
  };

  return (
      <div>
        <form onSubmit={handleSubmit}>
          <div >
            <label htmlFor="pseudo">Pseudo</label>
            <input name="pseudo" type="text" id="pseudo"/>
          </div>
          <div>
            <label htmlFor="password">password</label>
            <input name="password" type="password" id="password" autoComplete="true"/>
          </div>
          <div>
            <label htmlFor="mail">email</label>
            <input name="mail" type="email" id="mail"/>
          </div>
          <input
            type="submit"
            to="/user"
            value="Sign up"
          />
        </form>
      </div>
  );
}

2

Answers


  1. The object you pass to mongoose does not contain a field called mail instead you pass it as email. So you need to map the email field from your request payload to mail:

    const user = new User({
            mail: req.body.email,
            password: req.body.password,
            pseudo: req.body.pseudo,
     });
    

    The other thing you need to fix is your client side. You’re sending form-data instead of a json-body. Change it to:

    const data = {  pseudo: e.target[0].value, 
                    password: e.target[1].value, 
                    mail: e.target[2].value };
    
    axios.post(url, data) // rest of your code
    
    Login or Signup to reply.
  2. Make sure you’re getting values out of req.body. It seems to me that might be it. Try to log the values of req.body in your signup controller. Newer versions of Expressjs don’t use the bodyParser middleware anymore, instead you’d use app.use(express.urlencoded()) and app.use(express.json()). Can’t comment on your post yet, so that’s why this answer might be kinda a suggestion.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search