title: Node.js Cannot read property &#39;firstname&#39; of undefined
body: <p>I am new to node.js. I am trying to do create a registration page and save the entered values in database using mysql.</p>
  
  <p>I am able to do so by passing parameters on request header. But, it's always not a good idea to pass parameters in request header. So trying to pass parameters on Request body.</p>
  
  <p>I am using Postman to test my node.js service.</p>
  
  <p>Here's github link for my demo application</p>
  
  <p><a href="https://github.com/debasish283/node_gulp" rel="nofollow">https://github.com/debasish283/node_gulp</a></p>
  
  <p>What extra I need to do to pass parameters in request body?</p>
  
  <p>When passing parameters in request body I am getting the below mentioned error.</p>
  
  <blockquote>
    <p>TypeError: Cannot read property &#39;firstname&#39; of undefined <br>
    &nbsp; &nbsp;at Object.db.register
    (/var/projects/node_gulp/server/db.js:13:65) <br> &nbsp; &nbsp;at
    wrapper.register (/var/projects/node_gulp/server/wrapper.js:8:12) <br>
    &nbsp; &nbsp;at Layer.handle [as handle_request]
    (/var/projects/node_gulp/node_modules/express/lib/router/layer.js:95:5)
    <br> &nbsp; &nbsp;at next
    (/var/projects/node_gulp/node_modules/express/lib/router/route.js:131:13)
    <br> &nbsp; &nbsp;at Route.dispatch
    (/var/projects/node_gulp/node_modules/express/lib/router/route.js:112:3)
    <br> &nbsp; &nbsp;at Layer.handle [as handle_request]
    (/var/projects/node_gulp/node_modules/express/lib/router/layer.js:95:5)
    <br> &nbsp; &nbsp;at
    /var/projects/node_gulp/node_modules/express/lib/router/index.js:277:22
    <br> &nbsp; &nbsp;at Function.process_params
    (/var/projects/node_gulp/node_modules/express/lib/router/index.js:330:12)
    <br> &nbsp; &nbsp;at next
    (/var/projects/node_gulp/node_modules/express/lib/router/index.js:271:10)
    <br> &nbsp; &nbsp;at Function.handle
    (/var/projects/node_gulp/node_modules/express/lib/router/index.js:176:3)</p>
  </blockquote>
  
  <p>Code:
  <strong>server -> wrapper.js</strong></p>
  
  <pre><code>var express = require('express');
  var db = require('./db.js');
  var config = require('config');
  
  var wrapper = {
  register: function(req, res) {
  console.log('inside wrapper');
  db.register(req.body);
  }
  };
  module.exports = wrapper;
  </code></pre>
  
  <p><strong>routes -> index.js</strong></p>
  
  <pre><code>var router = require('express').Router();
  var wrapper = require('../server/wrapper');
  
  router.post('/register', wrapper.register);
  
  module.exports = router;
  </code></pre>
  
  <p><strong>app.js</strong></p>
  
  <pre><code>var express = require('express');
  var app = express();
  var port = process.env.PORT || 3000;
  var router = require('./routes');
  var bodyParser = require('body-parser');
  
  app.use('/', router);
  
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(bodyParser.json());
  
  app.listen(port, function() {
  console.log('Server started at ' + port);
  });
  </code></pre>
  
  <p><strong>server-> db.js</strong></p>
  
  <pre><code>var mysql = require('mysql');
  var config = require('config');
  
  var connection = mysql.createConnection({
  host: config.get('database.host'),
  user: config.get('database.user'),
  password: config.get('database.password'),
  database: config.get('database.database')
  });
  var db = {
  register: function(parameter) {
  var qry = 'insert into users values (' + "'" + parameter.firstname + "'" + ',' + "'" + parameter.lastname + "'" + ',' + "'" + parameter.username + "'" + ',' + "'" + parameter.bday + "'" + ',' + "'" + parameter.gender + "'" + ',' + "'" + parameter.password + "'" + ')';
  console.log(qry);
  connection.connect();
  connection.query(qry, function(err, rows, fields) {
  if (!err) {
          console.log('The solution is: ', rows);
          } else
              console.log('Error while performing Query.' + err);
      });
  
      connection.end();
  }
  };
  module.exports = db;
  </code></pre>