私は特定の約束で、私はそれがエラーをログに記録が、正常に動作しますが、その理由は、未定義の私を与えresult
何らかの理由で、次の.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);
});
に残りのデータを渡したい約束のチェーンを持っています私は、オブジェクトのプロパティ(reason.err
、reason.movie
)にアクセスしようとするが、私は、オブジェクトの理由を呼び出すとき、それは私にこれを与えたとき:だから私はreason
はALSであるmsg
プロパティを使用してエラーオブジェクトで見ることができるものから
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%' } }
をoエラー。
それから私の質問。エラーとムービーの両方を渡すためにobjを拒否した場合、どのように次のPromiseに両方の値を渡すことができますか?私は終わりreason.err
とreason.movie
理由オブジェクトを作成するコード全体を表示してください。想定されている 'console.log(reason)'の結果は 'reject({err:new Error( 'Error')、movie})'行とまったく同じではありません。 – Bergi
'resolve()'と 'reject()'は '.then()'に関連して 'readDB(file)'にチェーンされていますか? 「 '//以前の拒否と結果」とはどういう意味ですか? – guest271314
この拒否された約束を作成するのが 'parseQuery()'であると仮定すると、それは拒否 '理由'に何でも入れたいコードです。私はまた、これが本当に "失敗"ではなく、異なる戻り条件である場合は、解決された値がその両方のタイプの戻り値を処理して拒否状態を保存できるように変更することをお勧めします実際の故障の場合。 – jfriend00