Skip to content
Advertisement

UnhandledPromiseRejectionWarning: ValidationError

I’m running into a trouble since a few days. I’m learning the MEAN stack, but during creation of a user on mongo using mongoose schema, I have this problem :

(node:93337) UnhandledPromiseRejectionWarning: ValidationError: User validation failed: username: Path username is required., password: Path password is required., email: Path email is required.

Here’s my code :

The server part :

mongoose.connect('mongodb://localhost:27017/Wisebatt', err => {
  if (err) {
    console.log(`Not connected to db ${err}`)
  } else {
    console.log('Successfully connected to db')
  }
})

app.post('/register', (req, res) => {
    const user = new User();
    user.username = req.body.username;
    user.password = req.body.password;
    user.email = req.body.email;
    user.save();
    res.send('User created');
});

The UserSchema :

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const UserSchema = new Schema({
    username: { type: String, required: true, unique: true},
    password: { type: String, required: true },
    email: { type: String, required: true, unique: true},
});

module.exports = mongoose.model('User', UserSchema);

Here are the add-ons I’m using :

  • Express,
  • Nodemon,
  • Morgan,
  • Body Parser,
  • Mongo (With mongod running & Mongoose)

Advertisement

Answer

Okay I found the problem…

Clearly, the problem is due to one of these two :

  • The browser used,
  • The extension sending the POST request

Surprise, I tried with Postman, and the request successfully work. So all the code was great, the problem came from one of the two up.

So, that learned me a thing. If it’s not your code, It’s the software you’re using that can destroy all you have done

Advertisement