最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - UnhandledPromiseRejectionWarning: ValidationError - Stack Overflow

programmeradmin0浏览0评论

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)

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)
Share Improve this question edited May 27, 2018 at 1:59 Neil Lunn 151k36 gold badges355 silver badges325 bronze badges asked May 27, 2018 at 1:50 Yanis BendahmaneYanis Bendahmane 552 silver badges12 bronze badges 21
  • can you post the part where you setup the database (mongodb) on the server? I'm guessing you haven't got the right credentials or database. – Matt Pengelly Commented May 27, 2018 at 1:53
  • Sure, here it is : mongoose.connect('mongodb://localhost:27017/Wisebatt', (err) => { if(err){ console.log(`Not connected to db ${err}`); } else { console.log('Successfully connected to db'); } }); – Yanis Bendahmane Commented May 27, 2018 at 1:54
  • You're not setting a username/password to connect to MongoDB with your current DSN – Explosion Pills Commented May 27, 2018 at 1:57
  • can you console log req.body.username ? it might be undefined. – Matt Pengelly Commented May 27, 2018 at 1:57
  • 1 if req.body is undefined, then the problem could be (most likely) in the client code that you've failed to show - please show your client side code – Jaromanda X Commented May 27, 2018 at 2:14
 |  Show 16 more ments

4 Answers 4

Reset to default 2

try adding this to your express code prior to your routes. This is a middleware that will setup the req.body object when you send requests to the backend. (you'll also need to npm install --save body-parser)

const bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())

if youre using a rest client. make sure that you have a request header such as:

Content-Type: application/x-www-form-urlencoded

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

If you are using expressJS then make sure that you have to add this lines.

const express = require('express');
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

Specially this one,

app.use(express.urlencoded({ extended: true }));

This actually solved my problem.

Use:

app.use(express.urlencoded({ extended: true })); // this is for app.post to submit route.. 

in the app.js or index.js

发布评论

评论列表(0)

  1. 暂无评论