【问题标题】:Firebase Function Promise Chain not catching errorsFirebase 函数承诺链未捕获错误
【发布时间】:2018-07-23 13:11:17
【问题描述】:

我正在使用 Firebase 函数来检测何时将 Stripe 令牌添加到用户集合中,然后创建 Stripe 用户并为该用户创建订阅。

我遇到的问题是下面的代码中有一个错误,虽然我需要弄清楚那个错误是什么,但 Promise Chain 似乎没有捕捉到实际的错误。 登出的都是Function execution took 4375 ms, finished with status: 'connection error'

有谁知道为什么会这样。我可以让它记录错误的唯一方法是嵌套 Promises。

exports.createStripeUser = functions.firestore
  // A Stripe Token is created in a user, therefore they have agreed to pay
  // Create a User in Stripe, and then attach the subscription to the Stripe customer
  .document('users/{userId}/stripe/{stripeCollectionId}')
  .onCreate((snap, context) => {
    const stripeToken = snap.data().stripeToken;
    let customer;
    return admin.auth().getUser(`${context.params.userId}`)
    .then(userObj => {
      const user = userObj.toJSON();
      return stripe.customers.create({
        email: user.email,
        source: stripeToken
      })
    })
    .then(cust => {
      customer = cust;
      return db.collection('users').doc(context.params.userId).collection('plan').doc(context.params.userId).get()
    })
    .then(plan => {
      return stripe.subscriptions.create({
        customer: customer.id,
        items: [{plan: plan.data().plan}]
      })
    })
    .catch(e => {
      console.log("ERRR", e)
      throw new functions.https.HttpsError('unknown', e.message, e);
    })
  })

【问题讨论】:

    标签: javascript firebase google-cloud-functions


    【解决方案1】:

    将你的块包装在一个

    try {
    
    } 
    catch (e) { 
    // e holds the nested error message 
    } 
    

    在那里阻止并找出错误。

    【讨论】:

    • 好的,但是promise链没有捕获错误有什么原因吗?
    • 据我了解,这是因为链的捕获仅捕获其中一个块,而不是所有.then 的总体捕获块。我一直默认使用try catch,以避免出现像你这样的问题。
    • try { } 块中包含哪些内容?
    • 啊,没关系,firebase 代码中的错误。虽然现在功能说“好的”并且一切正常......这很奇怪......我不明白为什么它会起作用......
    猜你喜欢
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 2018-11-05
    • 1970-01-01
    • 2022-12-19
    • 2019-04-17
    • 2022-11-11
    • 1970-01-01
    相关资源
    最近更新 更多