我通过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
怎么了?
答案 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
,如上面的代码所示。