我正在尝试从我的 Node 应用程序中删除匿名函数。例如:
app.post('/page-edit.json', function (req, res) {
db.get(req.body.page_id, function (err, doc) {
res.contentType('json');
res.send(doc);
});
});
所以说我打破了内部函数:
function innerMost(err, doc) {
res.contentType('json');
res.send(doc);
}
function outer(err, doc) {
db.get(req.body.page_id, innerMost);
}
app.post('/page-edit.json', outer);
问题是,如何将额外的参数(例如“res”)传递到“innerMost”中?它在这个过程中迷失了。
如果你想查看源代码(或者甚至想为开源项目做出贡献!),你可以看到它 here !
最佳答案
这可能是你能用常规 JS 做的最好的事情了:
function outer(req, res) {
function innerMost(err, doc) {
res.contentType('json');
res.send(doc);
}
db.get(req.body.page_id, innerMost);
}
app.post('/page-edit.json', outer);
但是,您可能想研究各种异步库,例如 https://github.com/caolan/async 。如果您想进一步冒险,您可以考虑使用icedcoffeescript http://maxtaco.github.io/coffee-script/ ,我认为这相当不错。
关于javascript - 在express中传递额外参数 - Node.JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18244250/