title: Type error in mean stack post method
body: <p><strong>MEAN Stack</strong></p>
  
  <p>I keep getting </p>
  
  <blockquote>
    <p>TypeError: Cannot read property 'first_name' of undefined</p>
  </blockquote>
  
  <p>when trying to execute this code (all variables, including modules are defined in other parts of the code). While using GET Method, my code is working fine. But for POST Method, it throws error. Attached the screenshot below.</p>
  
  <p><a href="https://i.stack.imgur.com/0N502.png" rel="nofollow noreferrer">Advance Rest Client POST Method Exception</a></p>
  
  <p>Added my code below.</p>
  
  <p>Thanks in advance</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>//app.js
  //Importing Modules
  
  var express = require('express');
  var mongoose = require('mongoose');
  var bodyparser = require('body-parser');
  var cors = require('cors');
  var path = require('path');
  
  var app = express();
  
  //port Number
  const port = 3000;
  
  const route = require('./routes/route');
  
  //Connect to MongoDB
  mongoose.connect('mongodb://localhost:27017/contactlist');
  
  //if connected
  mongoose.connection.on('connected', () =&gt; {
    console.log("Mongo DB Connected successfully");
  });
  
  //if not connected
  mongoose.connection.on('error', (err) =&gt; {
    if (err) {
      console.log('Error in connecting to the MongoDB');
    }
  });
  
  //adding middleware cors
  app.use(cors());
  
  //routes will happen here
  app.use('/api', route);
  
  //adding middleware
  app.use(bodyparser.json());
  
  //Static files
  app.use(express.static(path.join(__dirname, 'public')));
  
  //Testing
  app.get('/', (req, res) =&gt; {
    res.send("Hello Vinoth");
  });
  
  app.get('/yahoo', (req, res) =&gt; {
    res.send("Hello Vinoth");
  });
  
  //Connection
  app.listen(port, () =&gt; {
    console.log("Server started at port:" + port);
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>//route.js
  const express = require('express');
  const router = express.Router();
  
  const Contact = require('../models/contacts');
  
  // retrieving details
  router.get('/contacts', (req, res, next) =&gt; {
    Contact.find(function(err, contacts) {
      res.json(contacts);
    });
  });
  
  // Add Contacts
  //Add contact
  router.post('/contact', (res, req, next) =&gt; {
    console.log('Insides');
    let newContact = new Contact({
      first_name: req.body.first_name,
      last_name: req.body.last_name,
      phone: req.body.phone
    });
  
    newContact.save((err, contact) =&gt; {
      if (err) {
        res.json({
          msg: 'Failed to add contact'
        });
      } else {
        res.json({
          msg: 'Contact added successfully'
        });
      }
    });
  });
  
  // Delete Contacts
  router.delete('/contact/:id', (req, res, next) =&gt; {
    Contact.remove({
      _id: req.params.id
    }, function(err, result) {
      if (err) {
        res.json(err);
      } else {
        res.json(result);
      }
    });
  });
  
  module.exports = router;</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>//contacts.js
  const mongoose = require('mongoose');
  
  const ContactSchema = mongoose.Schema({
    first_name: {
      type: String,
      required: true
    },
    last_name: {
      type: String,
      required: true
    },
    phone: {
      type: String,
      required: true
    }
  });
  
  const Contact = module.exports = mongoose.model('Contact', ContactSchema);</code></pre>
  </div>
  </div>
  </p>