在 Firebase 9 上的 Https Callable 函数中设置位置(条带客户门户)

tms*_*mst 1 javascript stripe-payments firebase google-cloud-platform google-cloud-functions

我正在尝试为用户设置一个按钮来管理他的 Stripe 客户门户及其 Firebase 集成的订阅。

问题,该文档对旧版本的 Firebase (8) 有效,而我正在使用新版本 Firebase 9。

我们在客户端,这就是 React.js。

这是他们调用该函数的js代码(您可以在这里看到完整的代码:https://github.com/stripe-samples/firebase-subscription- payments/blob/master/public/ javascript/app.js):

const functionLocation = 'us-east1';
    
const functionRef = firebase
      .app()
      .functions(functionLocation)
      .httpsCallable('ext-firestore-stripe-subscriptions-createPortalLink');
    const { data } = await functionRef({ returnUrl: window.location.origin });
    window.location.assign(data.url);
  });
Run Code Online (Sandbox Code Playgroud)

对于 Firebase 9,它应该是这样的:

const functions = getFunctions(firebaseApp)

export async function goToBillingPortal() {
       const functionRef = httpsCallable(functions, 'ext-firestore-stripe-subscriptions-createPortalLink');
    
       const { data } = await functionRef({ returnUrl: window.location.origin });
       window.location.assign(data.url);
 }
Run Code Online (Sandbox Code Playgroud)

控制台显示3个错误:

Access to fetch at 'https://us-central1-xxxxxxxxd76.cloudfunctions.net/ext-firestore-stripe-subscriptions-createPortalLink' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.

service.ts:206 POST https://us-central1-xxxxxxx76.cloudfunctions.net/ext-firestore-stripe-subscriptions-createPortalLink net::ERR_FAILED

Uncaught (in promise) FirebaseError: internal
Run Code Online (Sandbox Code Playgroud)

显然,问题是我需要指定服务器的位置(例如 us-central1),但我无法理解如何使用 Firebase 9 指定服务器的位置。所有指南均适用于 Firebase 8。

我尝试过不同的事情,例如:

const functions = getFunctions(firebaseApp).location("us-central1")
Run Code Online (Sandbox Code Playgroud)

或者

const functionRef = httpsCallable(functions, 'ext-firestore-stripe-subscriptions-createPortalLink').location("us-central1")
Run Code Online (Sandbox Code Playgroud)

但它不起作用。

有想法吗?

Ren*_*nec 6

如文档中所示您应该将该区域作为该getFunctions()方法的第二个参数传递:

 const functions = getFunctions(firebaseApp, "europe-west1");
Run Code Online (Sandbox Code Playgroud)