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: Pathpassword
is required., email: Path
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: Pathpassword
is required., email: Path
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)
- 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
4 Answers
Reset to default 2try 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