Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在回调函数中传递原始请求?_Node.js_Callback_Request - Fatal编程技术网

Node.js 如何在回调函数中传递原始请求?

Node.js 如何在回调函数中传递原始请求?,node.js,callback,request,Node.js,Callback,Request,我试图通过命名函数来避免回调地狱,但无法请求传递不属于原始?作用域的对象?父方法的 以下是我试图优化的工作代码: //req exists, as this is inside an Express route handler charge.create({ amount: req.body.amount //... }, function(err, createdCharge) { console.log('Purchased: '+req.body.produc

我试图通过命名函数来避免回调地狱,但无法请求传递不属于原始?作用域的对象?父方法的

以下是我试图优化的工作代码:

//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方法不是我创建的,所以我不能让它接受任何其他参数:

charge.create({
   amount: req.body.amount
   //...
}, confirmCharge);

function confirmCharge(err, createdCharge) {
      console.log('Purchased: '+req.body.product); //req doesn't exist!
}

create返回err和createdCharge,在调用confirmCharge时不需要指定它。当我输入confirmCharge(req)时,它会显示未知参数。如果您想要一个特定的现成工作解决方案,您需要发布费用代码。下面是我使用表单条带的示例var token=request.body.stripeToken;//使用Express//Charge对用户的卡进行充值:var Charge=stripe.charges.create({金额:1000,币种:“usd”,说明:“示例充值”,来源:token,},函数(err,Charge){//异步调用})`
charge.create({
   amount: req.body.amount
   //...
}, confirmCharge(req));

function confirmCharge(req) {
      console.log('Purchased: '+req.body.product); //req exists!
}