title: Return response from a module function node.js
body: <p>I have an app file with routes in it and another module written which has the mongoose schema and the model object of a collection. Code for app is:</p>
  
  <pre><code>app.post("/saveNode", function(req, res) {
      console.log(req.body.clientId)
      if(req.body.hasOwnProperty("clientId")) {
          defferedObject = nodeDbObject.returnCollectionObject(req.body);
      }
  }
  </code></pre>
  
  <p>From the route I am calling a function of mongoose <code>model(returnCollectionObject)</code> object which will save the <code>body</code> obtained in post call to the database.This function is written in the module. Code for the same is given below:</p>
  
  <pre><code>exports.returnCollectionObject = function(body) {
      var collection = "refresh_hierarchy";
      var nodeModelObject = mongoose.model(collection, folderSchema);
      return nodeModelObject.create(body, function(err, resp) {
          if(err == null) {
              return resp;
          }
      });
  };
  </code></pre>
  
  <p>I am calling this function from my route written for post. And i want the object returned from this <code>function(resp)</code> from inside my route so that i can use some of its fields to be returned in the <code>response</code>. Tried using <code>.done()</code> on the <code>defferedObject</code> but <code>return resp</code> does not return anything. Am i doing it right? Here is the code with done function.</p>
  
  <pre><code>app.post("/saveNode", function(req, res) {
      if(req.body.hasOwnProperty("clientId")) {
          defferedObject = nodeDbObject.returnCollectionObject(req.body);
          defferedObject.done(function(resp) {
              res.send({"data": resp.uniqueNodeId, "error": err});
              res.end();
          });
      }
  }
  </code></pre>
  
  <p>This is just my first crack at writing a node service.Can Anyone help me out with this.Can it be done like that or not? The error i am getting is as below.</p>
  
  <pre><code>TypeError: Cannot call method 'done' of undefined
  </code></pre>