Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.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
Javascript 如何将多个功能集成到我的发布路线和传递对象中_Javascript_Node.js - Fatal编程技术网

Javascript 如何将多个功能集成到我的发布路线和传递对象中

Javascript 如何将多个功能集成到我的发布路线和传递对象中,javascript,node.js,Javascript,Node.js,我想将StripeAPI集成到我的项目中。我已经收集了所有需要的数据,并将其发送到我的邮寄路线。 我想向客户收费,并让所有功能都这样做,如果我想逐一调用它们的话。如何将所有功能集成到我的post路线中,以便一次处理所有功能。此外,我不知道如何将数据从一个函数传递到另一个函数,因此最终会有一个具有相同数据的函数链。我的邮政路线和职能: router.post("/checkout", async function (req, res, next) { if (!req.session

我想将StripeAPI集成到我的项目中。我已经收集了所有需要的数据,并将其发送到我的邮寄路线。 我想向客户收费,并让所有功能都这样做,如果我想逐一调用它们的话。如何将所有功能集成到我的post路线中,以便一次处理所有功能。此外,我不知道如何将数据从一个函数传递到另一个函数,因此最终会有一个具有相同数据的函数链。我的邮政路线和职能:

     router.post("/checkout", async function (req, res, next) {
  if (!req.session.cart) {
    return res.redirect("/shopping-cart");
  }
  // You can return promise directly
  let createCustomer = function () {
    var param ={};
    param.email = req.body.email;
    param.name= req.body.name;
    param.description ="";
    return stripe.customers.create(param, function (err, customer) {
      if (err) {
        console.log("err:" + err);
      }
      if (customer) {
        console.log("success: " + JSON.stringify(customer, null, 2));
      } else {
        console.log("something went wrong");
      }
    });


  };

  let createToken = function () {
    let param ={};

    param.card = {
      number: req.body.card,
      exp_month: req.body.exp_month,
      exp_year: req.body.exp_year,
      cvc: req.body.security
  }
    return stripe.tokens.create(param, function (err, token) {
      if (err) {
        console.log("err:" + err);
        console.log(param);
      }
      if (token) {
        console.log("success: " + JSON.stringify(token, null, 2));
        console.log(req.body);
      } else {
        console.log("something went wrong");
      }
    });
  };




  let addCardToCustomer = function () {
    console.log(createdCustomer);
   return stripe.customers.createSource(customer.id, {source: token.id}, function (err, card) {
     if (err) {
       console.log("err:" + err);
       console.log(param);
     }
     if (card) {
       console.log("success: " + JSON.stringify(card, null, 2));
     } else {
       console.log("something went wrong");
     }
   });
 };



  try {
    const createdCustomer = await createCustomer(); // promise 1
    const createdToken = await createToken();
    const addedCardToCustomer = await addCardToCustomer(createdCustomer,createdToken ); // await addCardToCustomer(createdCustumer); to pass created customer info to next request
    // const chargeCustomerThroughCustomerID = await chargeCustomerThroughCustomerID(); // promise 3
    // more things...

    res.send("success");

  } catch (e) {
    console.log(`error ${e}`)
  };



});

你可以把你的承诺串起来…/使用async Wait,按照需要的顺序一次执行一项任务。您还可以将数据从一个承诺传递到另一个承诺,如下所示

//您可以直接返回承诺
让createCustomer=函数(){
返回条带.customers.create(参数);
}
设addCardToCustomer=函数(){
返回stripe.customers.createSource(customer.id,{source:token.id});
};
//或者使用异步/等待
让chargeCustomerThroughCustomerID=异步函数(){
const data=wait stripe.charges.create(param.catch)((e)=>{console.log(`error${e}`);throw e})
//处理数据
返回数据;
}
让chargeCustomerThroughTokenID=异步函数(){
const data=wait stripe.charges.create(param.catch)((e)=>{console.log(`error${e}`);throw e});
//处理数据
返回数据;
}
router.post(“/checkout”),异步函数(req,res,next){//async
如果(!req.session.cart){
返回res.redirect(“/购物车”);
}
var cart=新购物车(req.session.cart);
试一试{
const createdCustomer=等待createCustomer();//承诺1
const addCardToCustomer=wait addCardToCustomer();//wait addCardToCustomer(createdCustumer);将创建的客户信息传递给下一个请求
const chargCustomer=通过CustomerId()等待ChargeCustomer;//承诺3
//更多的事情。。。
res.send(…);
}
捕获(e){
log(`error${e}`)
}

});非常感谢。因此,我尝试了您对return promise Direct变量的建议,但我得到了以下错误ReferenceError:在初始化之前无法访问“createCustomer”:我是否做错了什么?你知道怎么回事吗?是的。。。如果你只是按原样使用代码,它将不起作用。您可以声明
let createCustomer=function(){
在底部。但是它在声明之前就已经被使用了。所以你需要移动到顶部。更新了答案是的,这是有意义的。现在我的新问题是:addCardToCustomer仍然作为第二个被调用。另外,createdCustomer在尝试传递它时没有定义。我会将我的问题中的新代码更新为第二个?如果你有问题与原来的不一样,那么您需要问一个新问题。并展开/关注StripeAPI创建部分