Reputation: 139
I am trying to register an user with expressjs/mongoose, but I am getting the error below:
TypeError: Cannot read property 'user_first_name' of undefined
at C:\Quiz webPolitica\server.js:20:24
at Layer.handle [as handle_request] (C:\Quiz webPolitica\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Quiz webPolitica\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch (C:\Quiz webPolitica\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Quiz webPolitica\node_modules\express\lib\router\layer.js:95:5)
at C:\Quiz webPolitica\node_modules\express\lib\router\index.js:277:22
at Function.process_params (C:\Quiz webPolitica\node_modules\express\lib\router\index.js:330:12)
at next (C:\Quiz webPolitica\node_modules\express\lib\router\index.js:271:10)
at serveStatic (C:\Quiz webPolitica\node_modules\express\node_modules\serve-static\index.js:75:16)
at Layer.handle [as handle_request] (C:\Quiz webPolitica\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Quiz webPolitica\node_modules\express\lib\router\index.js:312:13)
at C:\Quiz webPolitica\node_modules\express\lib\router\index.js:280:7
at Function.process_params (C:\Quiz webPolitica\node_modules\express\lib\router\index.js:330:12)
at next (C:\Quiz webPolitica\node_modules\express\lib\router\index.js:271:10)
at expressInit (C:\Quiz webPolitica\node_modules\express\lib\middleware\init.js:33:5)
at Layer.handle [as handle_request] (C:\Quiz webPolitica\node_modules\express\lib\router\layer.js:95:5)
I've tried debugging for hours and looked up other people's problems, but I ain't getting anywhere. Any help is really appreaciated. Please see my code below:
server.js
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/public'));
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/iPolitico');
var Register_User_Schema = new mongoose.Schema({
Firstname: { type: String, required: true },
Lastname: { type: String, required: true },
Email: { type: String, required: true, index: { unique: true } },
Password: { type: String, required: true }
}, {collection:"user"});
var Register_User = mongoose.model("Register_User", Register_User_Schema);
app.post('/register', function(req,res){
new Register_User({
Firstname: req.body.user_first_name,
Lastname : req.body.user_last_name,
Email : req.body.user_email,
Password : req.body.user_password
}).save(function(err, doc){
if(err){
res.json;
}else{
app.use(express.static(__dirname + '/user_profile.html'));
}
})
});
app.get('/', function(req, res){
//res.send('hello world');
Register_User.find(function (err, data){
res.json(data);
});
});
app.listen(3000);
index.html
<form role="form" action="/register" method="POST" class="registration_form"> <!--action="javascript:;" onsubmit="register_user()"-->
<div class="form-group">
<label class="sr-only" for="form-first-name">First name</label>
<input type="text" name="user_first_name" id="user_first_name" placeholder="First name..." class="form-first-name form-control" >
</div>
<div class="form-group">
<label class="sr-only" for="form-last-name">Last name</label>
<input type="text" name="user_last_name" id="user_last_name" placeholder="Last name..." class="form-last-name form-control">
</div>
<div class="form-group">
<label class="sr-only" for="form-email">Email</label>
<input type="text" name="user_email" id="user_email" placeholder="Email..." class="form-email form-control" >
</div>
<div class="form-group">
<label class="sr-only" for="form-password">Senha</label>
<input type="password" name="user_password" id="user_password" placeholder="Senha..." class="form-password form-control" >
</div>
<div class="form-group">
<label class="sr-only" for="form-password">Confirmar Senha</label>
<input type="password" name="user_confirm_password" placeholder="Confirmar Senha..." class="form-password form-control" >
</div>
<button type="submit">Sign me up!</button>
</form>
As a side question. When I am done registering I want to redirect to a new page. I currently have "app.use(express.static(__dirname + '/user_profile.html'));", but I am quite sure this is not correct or half correct.
Upvotes: 2
Views: 530
Reputation: 4308
You error simply says that the body in req.body is null. So after some Googling Stack Overflow returned me to this question. Basically it says that:
You must use something to parse the request's body data.
That means using something like bodyParser
But as @andrea.spot mentions in the comments, ever since Express4 (which I assume you are using) the BodyParser middleware must be installed separately.
So for Express4, @Jay's answer suggests you should use the following when configuring express:
var bodyParser = require('bodyParser');
app.use(bodyParser().json());`
Also, as @Jay points out, you can install it by calling:
npm install body-parser --save
Upvotes: 2
Reputation: 1350
The req.body
is actually handled by middleware in Express to convert the request data to JSON before your custom code accesses it.
Try including body-parser in your project (npm install body-parser
) and adding this before your first app.post:
var bodyParser = require('body-parser');
app.use(bodyParser.json());
This will grab the request to your server, parse out the data from your POST call, and put it in a JSON object as req.body.
As for redirecting, at the end of a call just add a simple res.redirect('/newPage')
Upvotes: 2