我试图通过命名函数来避免回调地狱,但我无法请求传递一个不属于原始范围的对象?父方法。
这是我正在尝试优化的工作代码:
//req exists, as this is inside an Express route handler
charge.create({
amount: req.body.amount
//...
}, function(err, createdCharge) {
console.log('Purchased: '+req.body.product);
res.send('Success');
}
当我尝试模块化我的代码时,我不确定如何传递req对象,因为charge.create方法是我没有做的,所以我不能让它接受任何其他参数:< / p>
charge.create({
amount: req.body.amount
//...
}, confirmCharge);
function confirmCharge(err, createdCharge) {
console.log('Purchased: '+req.body.product); //req doesn't exist!
}
答案 0 :(得分:0)
charge.create({
amount: req.body.amount
//...
}, confirmCharge(req));
function confirmCharge(req) {
console.log('Purchased: '+req.body.product); //req exists!
}