2017-10-19 37 views
0

我有地方在一定的承諾,我希望它記錄錯誤,但其餘數據傳遞到下一個.then()如何訪問下一個.then()中包含錯誤的被拒絕對象?

const parseQuery = (movies) => { 
    return new Promise((resolve, reject) => { 
     const queries = Object.keys(req.query).length; 
     if(debug) console.log('Parsing ', queries ,'queries'); 
     if(queries > 0) { //If there's any query run this 
     //When calling two parameters 
     if(req.query.index && req.query.trailer) reject(errorify('You can\'t call two parameters at once.')); 
     //When calling index 
     else if(req.query.index){ 
      var index = Number(req.query.index); 
      if(debug) console.log('Calling index ',index); 
      if(index<movies.length && index>=0){ //Index is a number and in range 
      movie = movies[index]; 
      movie.index = index; 
      } 
      else if(isNaN(index) || index <= 0 || index>movies.length) { 
      let index = random.exclude(0,movies.length-1); 
      movie = movies[index]; 
      reject({ 
       msg: errorify('Index is not a number or it\'s out of range.'), 
       movie //Add the var as a property 
      }); 
      } 
      if(debug) console.log('Requested: ', movie.title); 
      } 
     //When calling trailer 
     else if(req.query.trailer){ 
      movie = {title: req.query.trailer}; 
     } 
     resolve([movie]); //Pass the result as a one item array 
     } 
     else { 
     resolve(movies); //If no query is called just pass the movies through 
     } 
    }); 
    }; 

    readDB(file) 
    .then(parseQuery) 
     .then(
     result => { selectMovie(result); }, 
     reason => { console.log(reason.err); selectMovie(reason.movie); 
}); 

出於某種原因,承諾鏈,result工作正常,但理由讓我不確定當我嘗試訪問該對象的屬性(reason.errreason.movie),但它給了我這個當我打電話的對象的原因:

Error: { msg: 
    Error: Index is not a number or it's out of range. Selecting a random movie. 
     at errorify (/Users/gonzo/Projects/JS/random-movie-trailer/src/controllers/routes.js:16:10) 
     at Promise (/Users/gonzo/Projects/JS/random-movie-trailer/src/controllers/routes.js:61:20) 
     at Promise (<anonymous>) 
     at parseQuery (/Users/gonzo/Projects/JS/random-movie-trailer/src/controllers/routes.js:43:12) 
     at <anonymous> 
     at process._tickCallback (internal/process/next_tick.js:169:7), 
    movie: { title: 'The Hero', usersScore: '77%', criticsScore: '64%' } } 
從我所看到的 reason

所以是一個msg屬性,它是ALS Error對象o錯誤。

然後我的問題。如果拒絕一個obj來傳遞錯誤和電影是無法解決的,我怎麼能將這兩個值傳遞給下一個Promise?所以,我可以使用reason.errreason.movie

+1

請向我們展示創建原因對象的整個代碼。 'console.log(reason)'的假設結果與'reject({err:new Error('Error'),movie})'行根本不匹配。 – Bergi

+0

如何將'.then()'鏈接到'readDB(file)''相關的resolve()'和'reject()'? 「/ /上一個拒絕和結果」是什麼意思? – guest271314

+0

假設它是'parseQuery()'創建這個被拒絕的承諾,那就是可以把它想要的任何東西放到拒絕'reason'中的代碼。我還建議,如果這不是真正的「失敗」,而只是一種不同的回報條件,您可能需要改變解決的價值,以便您可以在那裏處理兩種類型的退貨並保存被拒絕的狀態一個實際的失敗。 – jfriend00

回答

0

最後我者優先不拋出一個錯誤,什麼jfriend00告訴我去:我現在發送的對象只是附加一個msg如果有一個「錯誤」,並在功能處理輸出我把一個if語句檢查對象是否具有msg屬性。像這樣:

if(movies.msg) { 
    console.log('Index :',movies.index); 
    console.error(movies.msg); 
    movies = [movies.movie]; 
} 

selectRandom(movies) 
    .then(requestTrailer)