JavaScript变量poofs在findById回调中不存在

时间:2015-05-23 03:36:44

标签: javascript node.js mongoose closures

在这段代码中,定义了Integer (Int64) :: memo(48) = 0 Integer (Int64), Intent (Out) :: fb Integer (Int8), Intent (In) :: n Integer (Int64) :: fb1, fb2 If (memo(n) > 1) Then ! if its in the array we just use that value fb = memo(n) Else If (n <= 2) Then memo(n) = Int (1, Int64) fb = memo(n) Else Call fibonacci_genr (fb1, n-1) Call fibonacci_genr (fb2, n-2) memo(n) = fb1 + fb2 fb = memo(n) End If End Subroutine fibonacci_genr 参数,但是当我进入回调时,它就不再存在了。不过,我仍然可以通过req(奇怪)找到它。发生了什么?我认为该功能将关闭其环境。

res.req

1 个答案:

答案 0 :(得分:1)

在javascript中,你这样做,只是

function addDocument(req, res) {
    // in scope
    Request.findById(req.body._id, function(error, request){
        if (request) {
            req.whatever; // What is Request.findById for ?
        } else {
            res.send404('Couldn\'t find a request with that ID.');
        }
    });
}