在这段代码中,定义了 req
参数,但是当我进入回调内部时,它就不再存在了。不过,我仍然可以通过 res.req 访问它(很奇怪)。发生了什么?我认为该函数会在其环境中关闭。
function addDocument(req, res) {
// in scope
Request.findById(req.body._id, function(error, request){
if (request) {
// disappeared
} else {
res.send404('Couldn\'t find a request with that ID.');
}
});
}
最佳答案
在 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.');
}
});
}
关于JavaScript 变量 poof 在 findById 回调中不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30408650/