2016-12-27 1 views
0

에 갖도록 나는이 개 콜백 내가 블루 버드의 then체인 연속 콜백 함수들은 모두 catch 블록

내 첫 콜백 함수를 사용하여 체인 싶습니다 몽구스의 기능을 성공적으로 then를 사용을 가지고 블루 버드를 사용하는 방법.

User.findOne().distinct(('Friends.id'), {id: req.body.myId}, {Friends: {$elemMatch: { gender: req.body.gender}}}) 
.then(function(IDs){ 

var results = //////some computation 

}) 
.catch(function(error)) { 


} 

첫 번째 콜백 함수의 catch 메서드를 공유하도록 두 번째 콜백 함수를 연결하는 구문 권한을 얻을 수 없습니다. 이 경우 두 번째 콜백 함수가 첫 번째 콜백 함수 인 results에 종속되어 있기 때문에 Promise.all을 사용할 수 없습니다. 어쨌든 두 번째 콜백 함수는 다음과 같습니다.

User.find({Friends: { $not: { $elemMatch: { id: req.body.myId }}}, id: {$in: results}}, function(err, users){ 



}) 

답변

1

이렇게 두 가지 약속을 연결할 수 있습니다.

User.findOne().distinct(('Friends.id'), {id: req.body.myId}, {Friends: {$elemMatch: { gender: req.body.gender}}}) 
    .then(function(IDs){ 

     var results = //////some computation 

     // second promise 
     return User.find({Friends: { $not: { $elemMatch: { id: req.body.myId }}}, id: {$in: results}}) 
    }) 
    .then(function(friends) { 
     // do something with the result of the second query 
    }) 
    .catch(function(error)) { 

    }