即使发生错误也保证执行继续

时间:2019-07-12 12:01:35

标签: javascript node.js firebase promise stripe-payments

我正在使用Firebase和Stipe付款构建一个应用程序。我创建了计费功能,如果成功,数据库将被更新。如果不是,则应该中断功能并引发错误。 而是抛出错误,但继续执行该功能。

我尝试使用.catch()解决此问题,但并没有停止该功能。

这是负责人和火力基地的更新代码:

app.post("/charge", verifyUser, (req, res) => {
  // console.log(req.body.stripeToken);
  const amount = 2500;
  const user = res.locals.decodedClaims;
  const uid = user.uid;
  stripe.customers
    .create({
      // email: res.locals.decodedClaims.email,
      email: req.body.stripeToken.email,
      source: req.body.stripeToken.id
    })
    .then(customer => {
      stripe.charges
        .create({
          amount,
          description: "Web Development Ebook",
          currency: "usd",
          customer: customer.id
        })
          //----------this is where the error happens if card is declined
            and i want here to stop the function execution
        .catch(err => {
          //error if card is declined or other reason
          return console.log("Payment Error: ", err.type, err.message);
        });
    })
    .then(charge => {

//this runs also if the other chargement fails
      const userRef = db.collection("users").doc(uid);
      let updatedStatus = userRef.update({ paid: true });

      updatedStatus.catch(err => {
        console.log(err);
      });

      res.json({ message: "Success!" });
    })
    .catch((err, customer) => {
      var msg = err || "unknown";
      console.log(msg);

      res.json({ message: msg });
    });
});

我想知道如何停止进一步执行该功能。

0 个答案:

没有答案