Node.jsの再帰的非同期リクエストの動作に関する質問があります。Node.jsでの非同期再帰に関する問題
以下の機能は、MongoDBのからの検索結果を返すことを意図しています。最初の検索結果が空になったら、テキストを個々の単語に分割してから、resオブジェクトをパラメータとして渡して、各単語のfetchResult(...)を再帰的に試みます。
function fetchResult(text, res){
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect(mongoURL + "/search", function (err, db) {
if(err) throw err;
db.collection('results', function(err, collection) {
// search for match that "begins with" text
collection.findOne({'text':new RegExp('^' + text, 'i')}, function(err, items){
var result = (items == null || items.result == null) ? "" : items;
if (result){
res.send(result);
}
else {
// no result, so fire off individual word queries - if any spaces found
if (text.indexOf(' ') > -1){
// split string into array
var textArray = text.split(" ");
// recursively process individual words
for (index = 0; index < textArray.length; index++) {
// ***** RACE CONDITION HERE? *****
fetchResult(textArray[index], res);
}
}
else {
// just return empty result
res.send(result);
}
}
});
});
});
}
は、私は、これはファンアウト非同期解像度を基準として、競合状態のビットを引き起こす可能性が疑われ、これは、私は、コードを実行したときに確認し、次のエラーが観察された:
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)
を だから私の質問は:どのように私は、個々の文字列を実行し、所望の再帰的な行動は、我々は(または検索が全く結果が返されないプロセスの終了時に)最初の結果を見つけたときにのみ返す、シーケンスでを照会達成することができますか?
研究 – Deep