2016-07-04 3 views
0

약속을 위해 node.js와 mongodb 및 q.js를 사용하고 있습니다.Node.js q.js를 사용하여 약속을 묶기

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

내 쿼리는 다음과 같습니다 :

  1. 이이 약속의 chainging을 구현하는 올바른 방법인가

    다음은 내 MongoDB의 스키마입니까?

  2. 마지막 체인에서 비어있는 결과가 나오지만 콘솔에 o/p를 인쇄하면 올바른 결과를 얻습니다.
  3. getEducationDetail 함수의 반복을 수행하는 방법은 올바른 방법인지 또는 다른 방법이 있는지 확인하십시오. 그렇다면 어떻게해야 동일한 결과를 얻을 수 있습니까? 당신이 코드는 청소기가 될 수
+1

당신은 finalResult' 기적'반환하는 자바 스크립트 ES6 사용 화살표 기능을 사용하여 단축 할 수 있지만 값 비동기식으로 만 채워집니다. – thefourtheye

답변

0

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

처럼 일부를 조정하십시오 당신은 _.each을 대체하고 대신 q.all를 사용하는 경우 위의 코드는 또한 청소기가 될 수 있습니다.

  • 주의 사항 : 당신은 당신의 코드가 상당히 긴의 자바 스크립트 ES5를 사용하는 대신 function(x, y) {}
관련 문제