title: Need to save text box data of html in mongodb and display the saved data in browser using angularjs, expressjs , nodejs
body: <p>Client-side : html code</p>
  
  <pre><code>  &lt;body ng-controller="myAppController"&gt;
  
  &lt;nav class="navbar navbar-inverse"&gt;
    &lt;div class="container-fluid"&gt;
      &lt;div class="navbar-header"&gt;
        &lt;h1&gt;person details&lt;/h1&gt;
      &lt;/div&gt;
  
    &lt;/div&gt;
  &lt;/nav&gt;
     &lt;div&gt; 
      &lt;form name="myform" ng-submit = "save()"&gt;
  
          &lt;label&gt;Enter your details&lt;/label&gt;&lt;br&gt;
          &lt;input type ="text" name ="name" placeholder="enter name.."&gt;
          &lt;input type ="text" name="surname"  placeholder ="enter surname..."&gt;&lt;br&gt;
          &lt;input type ="number" name="age"  placeholder ="enter age.."&gt;&lt;br&gt;
          &lt;input type="submit" value="submit"&gt;&lt;br&gt;
  
      &lt;/form&gt;
  
      &lt;/div&gt;
  
  &lt;div class="container-fluid text-center "&gt;    
  
      &lt;div class="col-sm-8 text-left" ng-repeat ="person in people"&gt; 
      &lt;ul&gt;
          &lt;li&gt;
      {{person.name}} {{person.surname}} {{person.age}}
          &lt;/li&gt;
      &lt;/ul&gt;
      &lt;/div&gt;
  
    &lt;/div&gt;
  </code></pre>
  
  <p>client-side: angular code</p>
  
  <pre><code>var myApp = angular.module('myApp',[]);
  
  myApp.controller('myAppController',['$scope', '$http',function($scope, $http, myAppService){
  
      $scope.people = [];
      $scope.init = function(){
          $http.get('http://localhost:3000/person/name').then(function(result){
              $scope.people = result.data;
          });
      }
      $scope.init();
  
       $scope.save = function () {
        $http.post('http://localhost:3000/add').then (function() {
          var formData ={name: this.name,
          surname: this.surname, 
          age: this.age};
            this.name = '';
            this.surname = '';
            this.age = '';
  
      var jdata = 'mydata='+JSON.stringify(formData);
  
          })
      }
      $scope.save();
  }]);
  </code></pre>
  
  <p>Sever side expressjs code, i guess the problem is here and in angular js code, I am able to save the first document created inside factoryjs and it displays in browser, but what ever i enter in to text box is not saved and gives following error:  angular.js:11821 POST <a href="http://localhost:3000/add" rel="nofollow noreferrer">http://localhost:3000/add</a> 500 (Internal Server Error)</p>
  
  <pre><code>var express = require('express');
  //var cors = require('cors-anywhere');
  var app = express();
  var mongoose = require('mongoose');
  
  var Schema = mongoose.Schema;
  var ObjId = Schema.ObjId;
  
  var Factory = require('./model/factory.js');
  
  mongoose.connect('mongodb://localhost/helloworld');
  
  var db = mongoose.connection;
  
  //adding control to make sure mongodb works fine
  
  db.on('error', function(){
      console.log("connection error");
  });
  
  db.once('open', function(){
     console.log("Mongo Working") 
  });
  
  var factory = new Factory(Schema, mongoose);
  factory.createSchema();
  factory.insertPeople();
  
  
  app.get('/ping',function(req,res){
      res.send('hello world this is from server!');
  });
  
  app.get('/:id', function(req, res){
      res.send('hello world this is from server '+req.params.id);
  });
  
  app.post('/add', function (req, res){
  
    console.log(req.body);
    console.log(req.body.mydata);
    var jsonData = JSON.parse(req.body.mydata);
    console.log(jsonData.name);
    console.log(jsonData.surname);
    console.log(jsonData.age);
  
    db.helloworld.save({name: jsonData.name, surname: jsonData.surname, age: jsonData.age}, function(err, saved) {
    if( err || !saved ) res.end( "User not saved"); 
    else res.end( "User saved");
  });
  });
  
  app.get('/person/name', function(req, res){
  
      var resp = factory.getPerson({name:'mani'}, res);
  });
  
  app.use(express.static(__dirname + '/public'));
  app.use(function(req, res, next){
      res.header('Access-Control-Allow-Origin',"*");
      res.header('Access-Control-Allow-Methods','GET,PUT,POST,DELETE');
      res.header('Access-Control-Allow-Headers', 'Content-Type');
      next();
  
  });
  
  app.get('/', function(req, res){
      res.render('index');
  })
  app.listen(3000);
  
  console.log('listening to port 3000....');
  </code></pre>
  
  <p>Mongoose model--factory.js to insert first created one and to save it</p>
  
  <pre><code>var Factory = function(Schema, mongoose){
  
  this.Schema = Schema;
  this.mongoose = mongoose;
  
  this.createSchema = function(){
  
      PersonSchema = new this.Schema({
  
          name: String,
          surname: String,
          age: Number
      });
  
      this.Person = mongoose.model('Person', PersonSchema);
  }
  this.insertPeople = function(){
  
      var mani = new this.Person({
          name: 'mani',
          surname: 'm*****',
          age: 25
      });
  
      mani.save();
  }
  this.getPerson = function(query, res){
  
      this.Person.find(query, function(err, output){
          res.json(output);
      })
  }
  
  }
  
  module.exports = Factory;
  </code></pre>