2013-05-08 62 views
0

以下代碼有什麼問題?我收集了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)。

回答

0

@JohhnyHK是正確的。除了懷疑您的查詢返回的是非空值以外,沒有其他解釋爲什麼Candy從未出現在您的列表中。

該測試通過(使用摩卡):

var async = require('async'), 
    should = require('should'), 
    sinon = require('sinon'); 


describe('async map', function() { 
    var Fruit, Vegetables, Candy, service; 
    var noop = function() {}; 

    beforeEach(function() { 
    Fruit = sinon.stub({ findOne: noop }); 
    Vegetables = sinon.stub({ findOne: noop }); 
    Candy = sinon.stub({ findOne: noop }); 

    }); 


    it('should map', function (done) { 

    var testlist = ["Tomato", "Carrot", "Orange", "Chocolate"]; 

    // return null record for everything 
    Fruit.findOne.yields(null); 
    Vegetables.findOne.yields(null); 
    Candy.findOne.yields(null); 

    // return some value when query matches (simulating mongo queries basically) 
    Fruit.findOne.withArgs({name: 'Orange'}).yields(null, 'Orange'); 
    Vegetables.findOne.withArgs({name: 'Tomato'}).yields(null, 'Tomato'); 
    Vegetables.findOne.withArgs({name: 'Carrot'}).yields(null, 'Carrot'); 
    Candy.findOne.withArgs({name: 'Chocolate'}).yields(null, '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] || foods[2]); 
     }); 
    }, function (err, result) { 

     var convertedList = [].concat(result); 
     convertedList.should.eql(testlist); 
     done(); 
    }); 

    }); 
}); 
+0

不要忘記upvote,如果它幫助! :) – Mrchief 2014-09-09 04:33:35

0

這條線在你async.parallel回調不沿結果使從Candy查詢:

next(err, foods[0] || foods[1]); 

試試這個:

next(err, foods[0] || foods[1] || foods[2]); 
+0

沒有與修正路線,同樣的問題開展工作。如果我重新命令下一個(err,食物[2] ||食物[0] ||食物[1]),那麼只有Candy出現。其他人是空的。 – Rolando 2013-05-08 13:29:49