【问题标题】:Firebase Functions error when calling Stripe调用 Stripe 时出现 Firebase 函数错误
【发布时间】:2020-08-08 07:55:24
【问题描述】:

我是一名 iOS 开发人员,在 Javascript 和服务器代码方面的经验非常少,所以我在这里有点迷茫。

当我在 firebase 中创建新用户并触发函数以在 stripe 中创建新用户时出现错误。这是我直接来自 Stripe 文档的 firebase 函数。

exports.createStripeCustomer = functions.auth.user().onCreate(async (user) => { 
    const customer = await stripe.customers.create({email: user.email});
    return admin.firestore().collection('stripe_customers').doc(user.uid).set({customer_id: customerId});
  });

我使用客户 ID 在 Stripe 中成功创建了一个新用户。 我在我的 firebase 日志中得到这个错误,并且没有捕获客户 ID,因此我可以将其保存在 firestore 中。 我不确定我做错了什么或如何解释此消息。任何指针将不胜感激。

createStripeCustomer
ReferenceError: customerId is not defined at exports.createStripeCustomer.functions.auth.user.onCreate (/srv/index.js:120:93) at <anonymous> at process._tickDomainCallback (internal/process/next_tick.js:229:7)

我也试过这个return将customerId更改为ID

return admin.firestore().collection('stripe_customers').doc(user.uid).set({customer_id: ID});

【问题讨论】:

    标签: javascript ios swift google-cloud-firestore stripe-payments


    【解决方案1】:

    documentation 看来,响应对象包含一个 id 属性。也许您打算改写这一行:

    return admin.firestore()
        .collection('stripe_customers')
        .doc(user.uid)
        .set({customer_id: customer.id});  // use the ID property here
    

    【讨论】:

    • 感谢您的想法 - 仍然收到此错误 createStripeCustomer TypeError: Cannot read property 'id' of undefined at exports.createStripeCustomer.functions.auth.user.onCreate (/srv/index.js:123:33) at &lt;anonymous&gt; at process._tickDomainCallback (internal/process/next_tick.js:229:7)
    • 所以你是说 stripe 从它的 API 返回了一个未定义的值?
    • 即使我看到在 Stripe 仪表板中创建了一个客户 ID,但似乎 stripe 没有返回客户 ID
    • 在 Stripe 中创建客户后是否可以登录 customer.id?如果您使用await,则没有理由之后customer 将未定义。可以分享一下使用customer.id时修改后的sn-p吗?
    • 另外,你returning firebase set() 行有什么原因吗?我对 Firebase auth 事件文档 [0] 的阅读并不表明对返回值有任何期望,因此请尝试将其删除。 [0] firebase.google.com/docs/functions/…
    【解决方案2】:

    这是我最终结束的地方。我使用了 promise 而不是 await。

    exports.createStripeCustomer = functions.auth.user().onCreate(async (user) => { 
      const stripePromise = new Promise((resolve, reject) => {
        stripe.customers.create({
          email: user.email
        }, (err, customer) => {
          if (err) {
            reject(err)
          } else {
            resolve(customer);
              stripePromise
                .then(customer => {
                  return admin.firestore()
                    .collection('stripe_customers')
                    .doc(user.uid)
                    .set({customer_id: customer.id});
                })
                .catch(error => { 
                  console.log(`error resolving promise ${error}`)
                })
          }
        });
      })
    });

    【讨论】:

      猜你喜欢
      • 2020-02-03
      • 2020-08-21
      • 1970-01-01
      • 2021-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-21
      • 2023-01-14
      相关资源
      最近更新 更多