2016-07-04 11 views
0

Ich benutze node.js mit mongodb und q.js für Versprechungen.Node.js Verkettung verspricht mit q.js

Es folgt mein mongodb Schema:

{ 
    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(); 
} 

Meine Frage ist:

  1. Ist dies der richtige Weg variert werden Versprechungen zu implementieren?
  2. In der letzten Kette von dann bekomme ich leeres Ergebnis, aber wenn ich das o/p zur Konsole drucke, bekomme ich rechtes Ergebnis.
  3. Die Art, wie ich Iteration der getEducationDetail-Funktion gemacht habe, ist es richtig oder gibt es eine Alternative. wenn ja, wie kann ich das gleiche erreichen.
+1

Sie kehren 'finalResult' synchron, aber die Werte werden nur asynchron bevölkert. – thefourtheye

Antwort

0

Sie Code sauberer sein können, bitte einige Teile wie diese

var updateUserDetails = function (req, res) { 
    var finalResult = []; 
    /* This is the function to update user using promise.*/ 
    return updateUserBasicDetails(req) 
    .then(function (req) { 
     _.each(req.body.userEducation, function (sd) { 
     return getEducationDetail(req.body._id, sd) 
      .then(function (result) { 
      if (result) { 
       return updateEducationDetails(sd, req.body._id) 
      } 
      else { 
       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(function (e) { console.log(e); }) 
    .done(); 
} 

und der obige Code kann auch sauberer, anpassen, wenn Sie _.eachq.all stattdessen ersetzen und verwenden.

  • Anmerkung: Sie Javascript ES5 verwenden, die Ihren Code sehr lange machen, können Sie es mit Hilfe von JavaScript es6 und Funktion mit dem Pfeil verkürzen statt function(x, y) {}