在Firebase身份验证上创建条带帐户

时间:2020-05-28 11:23:57

标签: javascript firebase firebase-authentication stripe-payments

我通过Firebase身份验证添加新帐户时,向Firebase云功能添加了一个功能,以创建条带化帐户

exports.syncUserToStripe = functions.auth
.user()
.onCreate(async (data, context) => {
cors(data, context, async () => {
  const stripeCustomer = await stripe.customers.create({
    email: data.body.email
  });
  context.send({ customer });
});

});

但出现错误

TypeError: Cannot read property 'origin' of undefined

怎么了?

1 个答案:

答案 0 :(得分:1)

您的功能是由身份验证事件(特别是在创建用户时)而非HTTP请求触发的。

CORS和context对象是HTTP概念,因此不应在此处使用。

更正的代码:

exports.syncUserToStripe = functions.auth
.user()
.onCreate(async (user) => {
  const stripeCustomer = await stripe.customers.create({
    email: user.email
  });

  // Do something with the Stripe customer object, like
  // save to Firestore or the realtime database?
});

此外,您还尝试从data.body.email访问用户的电子邮件。 Firebase用户对象不包含body键。您可以直接从用户对象(email)上移除user.email,如上面的代码所示。