[英]How do you pass variable to callback function?
New to node, express, and javascript. 是Node,Express和javascript的新功能。
Below is code in my index.js
in routers
folder. 下面是我的index.js
在routers
文件夹中的代码。
I want to pass along or access res
variable in handleImport
function. 我想传递或访问handleImport
函数中的res
变量。
function handleImport( err, stat)
{
if(err == null) {
// Do something with res
res.end();
}
else{
// Do something with res
res.end();
}
}
router.get('/traveler', function(req, res, next) {
var import_path = req.query.drawing;
fs.stat(import_path,handleImport);
});
It appears bind() may be what I'm after but cannot find a good source for how to use it correctly. 似乎bind()可能是我想要的,但是找不到如何正确使用它的良好来源。
AFAICS, the best solution is to use anonymous function. AFAICS最好的解决方案是使用匿名函数。
router.get('/traveler', function(req, res, next) {
var import_path = req.query.drawing;
fs.stat(import_path, function (err, stat) {
if (err == null) {
// Do something with res
res.end();
} else {
// Do something with res
res.end();
}
});
});
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.