以下代碼有什麼問題?我收集了mongo中的水果,蔬菜和糖果。 「testlist」包含含有食物的字符串,這些食物屬於三種類別之一,可以在藏品中查找。使用以下代碼在nodejs + mongoose + async中執行同步時出現問題?
出於某種原因,轉換後的列表從未出現由「糖果」組成,但僅包含水果和蔬菜。
var async = require("async"),
testlist = ["Tomato", "Carrot", "Orange", "Chocolate"];
async.map(testlist, function (food, next) {
async.parallel([function (done) {
Fruit.findOne({"name": food}, done);
},
function (done) {
Vegetables.findOne({"name": food}, done);
},
function (done) {
// The line below appears to execute successfully but never end up in "convertedList"
Candy.findOne({"name": food}, done);
}
], function (err, foods) {
next(err, foods[0] || foods[1]);
});
},
function (err, result) {
var convertedList = [].concat(result);
res.send(convertedList);
});
爲什麼Candy沒有被添加到生成的「convertedList」中?我該如何解決這個問題?
注意:我注意到,當我重新安排糖果和蔬菜的功能(完成)調用時,看起來蔬菜不會被添加到最終轉換列表中,但是糖果會。它似乎總是被添加到convertedLIst中被忽略的第三個函數(done)。
不要忘記upvote,如果它幫助! :) – Mrchief 2014-09-09 04:33:35