2014-09-04 14 views
2

我有以下功能我一直得到error: uncaughtException: Cannot call method 'redirect' of undefinedres.redirect即將寫在server.js爲未定義

function(req, res) { 
    async.waterfall([ 
    function(next){ 
     next(null, res); 
    }, 

    function(next){ 
     next(null, res); 
    }, 
    function(next, res){ 
     res.redirect('www.google.com') 
    } 
    ]); 
} 

使得從上面的回答下面我得到改變之後,錯誤如下:

2014-09-04T13:56:08.678Z - error: uncaughtException: Object function (err) { 
       if (err) { 
        callback.apply(null, arguments); 
        callback = function() {}; 
       } 
       else { 
        var args = Array.prototype.slice.call(arguments, 1); 
        var next = iterator.next(); 
        if (next) { 
         args.push(wrapIterator(next)); 
        } 
        else { 
         args.push(callback); 
        } 
        async.nextTick(function() { 
         iterator.apply(null, args); 
        }); 
       } 
      } has no method 'send' 
+2

您是否將'res'傳遞給函數? – loveNoHate 2014-09-04 12:07:06

+0

我的函數寫得和上面一樣 – jyoti 2014-09-04 12:07:49

+0

你是否通過從'.createServer'傳遞'res'來調用匿名函數?或者它在另一個函數中? – loveNoHate 2014-09-04 15:24:51

回答

5

爲什麼你需要將res傳遞給瀑布參數?您可以在全球範圍內使用它,就像這樣

function(req, res) { 
    async.waterfall([ 
    function(next){ 
     next(); 
    }, 

    function(next){ 
     next(); 
    }, 
    function(){ 
     res.redirect('www.google.com') 
    } 
    ]); 
} 
+0

在這種情況下,爲什麼不能完全刪除'async.waterfall'? – 2014-09-04 18:06:17

+0

因爲如果你需要按照順序和順序執行任務並將結果從一個任務傳遞給下一個任務,它將會保護你免受回撥地獄的影響。 – m2gikbb 2014-09-05 13:05:34

+0

我的意思就是在這種情況下。但我知道你的意思。 – 2014-09-05 13:10:47

相關問題