首页 > 解决方案 > Firebase 函数承诺链未捕获错误

问题描述

我正在使用 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);
    })
  })

标签: javascriptfirebasegoogle-cloud-functions

解决方案


将你的块包裹在一个

try {

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

阻止并在那里找到错误。


推荐阅读