问题描述
我正在构建一个云功能,它将使用Stripe API来处理付款.这是在firebase项目中.当我运行firebase deploy
时,出现错误对象是可能的'未定义'" const existingSource = customer.sources.data.filter( (s) => s.id === source).pop();
我不确定该如何解决.
I'm building a cloud function that will use the Stripe API to process payments. This is within a firebase project. When I run firebase deploy
I get the error "Object is possible 'undefined'" const existingSource = customer.sources.data.filter( (s) => s.id === source).pop();
I'm not sure how to resolve this.
这是我的xxx.ts,其中存在getorCreateCustomer
Here is my xxx.ts where getorCreateCustomer exists
/** Read the stripe customer ID from firestore, or create a new one if missing */
export const getOrCreateCustomer = async(uid: string) => {
const user = await getUser(uid);
const customerId = user && user.stripeCustomerId;
//if missing customerId, create it
if (!customerId) {
return createCustomer(uid);
}
else {
return stripe.customers.retrieve(customerId);
}
}
推荐答案
7个月后,我找到了最佳解决方案.
7 months later, I figured out the best solution.
我只是在以下if/else语句中包装了Firebase可调用函数的内容.有点多余,但是可以用.
I simply wrapped the the contents of the firebase callable function in the following if/else statement. It's a bit redundant but it works.
if (!context.auth) {
// Throwing an HttpsError so that the client gets the error details.
throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
'while authenticated.');
}
else{ ...copy function code here }
如果您不关心身份验证项,则可以简单地将上下文类型定义为 any .
If you don't care about the authentication piece you can simply define the type of context as any.
(data, context:any)
这篇关于如何解决打字稿错误“对象可能是'未定义'";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!