title: Node.js Chaining promises using q.js
body: <p>I am using node.js with mongodb and q.js for promises.</p>
  
  <p>Following is my mongodb schema :</p>
  
  <pre><code>{
    UserId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User'
    },
    FirstName: String,
    LastName: String,
    Gender: String,
    userDocument: [userDocumentSchema],
    userEducation: [userEducationDetailsSchema]
  };
  var userEducationDetailsSchema = new Schema({
    DegreeName: String,
    University: String,
  });
  var userDocumentSchema = new mongoose.Schema({
    DocumentName: String,
    DocumentPath: String
  });
  
  
  I have following function by chaining promises:
  
  var updateUserDetails = function (req, res) {
    var finalResult = [];
    /* This is the function to update user using promise.*/
    updateUserBasicDetails(req).then(function (req) {
      _.each(req.body.userEducation, function (sd) {
        return getEducationDetail(req.body._id, sd) /* this fun finds if education details are present or not by iterating usertEducation Array */
          .then(function (result) {
            if (result) {
              /* if education details exist then this will update the details */
              return updateEducationDetails(sd, req.body._id).then(
                function (result) {
                  return result;
                })
            } else {
              /*else add the education details*/
              return addEducationDetails(sd, req.body._id).then(
                function (result) {
                  return result;
                })
            }
          }).then(function (result) {
            finalResult.push(result);
          })
      })
      return JSON.stringify(finalResult);
    }).then(function (finalResult) {
      res.json({
        "token": finalResult /*but here m getting empty result */
      })
    }).catch(console.log).done();
  }
  </code></pre>
  
  <p>My query is :</p>
  
  <ol>
  <li>Is this the right way to implement chainging of promises ?</li>
  <li>In last chain of then I am getting empty result but when i print the o/p to console i get right result.</li>
  <li>The way i have done iteration of getEducationDetail function , is it right way or is there any alternative. if so how can i achieve the same.</li>
  </ol>