title: How to use Mongoose save method in Express POST route
body: <p>I am new to Express and Node and have a problem I do not understand.  I have defined two different Express routes to take a POST request for two different Mongoose data models.  One works fine and return properly, the other one returns a status of 204 in the Heroku logs and then times out, like the save is not even executing...</p>
  
  <p>Here is the "Yak" model and associated POST route which saves the object and returns the expected result back to my ANgular client: </p>
  
  <p>Model definition:</p>
  
  <pre><code>var mongoose = require('mongoose');
  var Schema = mongoose.Schema;
  
  var YakSchema   = new Schema({
  catagory: String,
  loc: {
      type: [Number],  // [&lt;longitude&gt;, &lt;latitude&gt;]
      index: '2d'      // create the geospatial index
  },
  yakName:String,
  yakDescription:String,
  yakPhone:String,
  yakOwner:String
  });
  
  module.exports = mongoose.model('Yak', YakSchema);
  </code></pre>
  
  <p>And the POST route</p>
  
  <pre><code>router.route('/yaks')
  
  .post(function(req, res) {
      var yak = new Yak();
  
      //First, need to get lat, lng from the address
      var addressString = req.body.city + ", " + req.body.street + ", " + req.body.state + ", " + req.body.postalcode;
      geocoder.geocode(addressString, function (err, data) {
  
          //error handling needed
          if (err){
              res.status(500).json({
                  status:500,
                  error:err
              });
              res.end();
          }
  
          var coord = data['results'][0]['geometry']['location'];
          var lat = coord.lat;
          var lng = coord.lng;
  
          //Second, use the Model to save the yak with a geoJSON point
  
          yak.catagory = req.body.catagory;
          yak.loc = [lng, lat];
          yak.yakName = req.body.yakName;
          yak.yakDescription = req.body.yakDescription;
          yak.yakPhone = req.body.yakPhone;
          yak.yakOwner = req.body.yakOwner;
          yak.save(function (err) {
              if (err) {
                  res.status(500);
                  res.json({
                      status: 500,
                      error: err
                  });
                  res.end();
              }
              else {
                  res.json({
                      status: 200,
                      yak: yak
                  });
                  res.end();
              }
          });
      });
  });
  </code></pre>
  
  <p>Here is the User model and user route which does NOT work</p>
  
  <pre><code>var mongoose = require('mongoose');
  var Schema = mongoose.Schema;
  
  
  var UserSchema = new Schema({
  username: { type: String, required: true, index: { unique: true } },
  password: { type: String, required: true }
  });
  
  module.exports = mongoose.model('User', UserSchema);
  </code></pre>
  
  <p>And the POST route</p>
  
  <pre><code>   router.route('/users')
      .post(function(req, res) {
  
  
      console.log(req.body.username);
      console.log(req.body.password);
  
  
      var user = new User();
      user.username = req.body.username;
      user.password = req.body.password;
  
      console.log(user);
  
      user.save(function(err){
  
          if (err) {
              res.status(500);
              res.json({
                  status: 500,
                  error: err
              });
              res.end();
          }
          else {
              res.json({
                  status: 200,
                  user: user
              });
              res.end();
          }
      });
  });
  </code></pre>
  
  <p>The only difference I see is that the save method for the "yak" route is wrapped in a callback for a previously executed method that geocodes a location.  THe User route is not in a call back.  </p>
  
  <p>This "feels" to me to have something to do with the Asynchronous nature of node but being new I am not sure.  Any help is greatly appreciuated</p>
  
  <p><strong>EDIT*</strong></p>
  
  <p>Here is the result of the console .log in the POST route but before the save.  I did add a console.log inside the save but it never gets logged out... </p>
  
  <p>Here is the result of the console.log of the username and password INSIDE the POST route but before the save.  I did add a console.log to the save method and it never got called....</p>
  
  <pre><code>heroku[router]: at=info method=OPTIONS     path="/api/users" host=frozen-       
  peak-5921.herokuapp.com request_id=6230237d-7adc-4a51-8a26-03da99f8b7e3    
  fwd="74.202.146.157" dyno=web.1 connect=2ms service=14ms status=204 bytes=289
   app[web.1]: asd
   app[web.1]: me@mail.com
   app[web.1]: { password: 'asd',
   app[web.1]:   username: 'me@mail.com',
   app[web.1]:   _id: 5612e098a7f49c1100000001 }
   heroku[router]: at=error code=H12 desc="Request timeout" method=POST 
  path="/api/users" host=frozen-peak-5921.herokuapp.com request_id=f6c0fde8-   
  c2e4-49e5-ad65-ba6afed3b731 fwd="74.202.146.157" dyno=web.1 connect=1ms 
  service=30001ms status=503 byte
  </code></pre>