【问题标题】:Firebase cloud function onCall not working after changing regionFirebase云功能onCall在更改区域后不起作用
【发布时间】:2019-06-20 17:56:12
【问题描述】:

我构建了几个像这样的云功能:

const addRoom = functions.https.onCall((data, context) => {

效果很好,但我想将区域更改为欧洲西部。我关注了这个stackoverflow:firebase deploy to custom region (eu-central1)

const addRoom = functions.region('europe-west1').https.onCall((data, context) => {

它看起来适用于所有函数(触发器)onCall 函数除外。在客户端调用 addRoom 函数时出现此错误:

firebase.functions().httpsCallable("addRoom")(data)

访问获取 'https://us-central1-myproject.cloudfunctions.net/addRoom' 来自原点 'http://localhost:4200/new-room' 已被 CORS 策略阻止: 对预检请求的响应未通过访问控制检查: 预检请求不允许重定向。

目前我为 onCall 函数使用默认区域,但有没有办法纠正这个问题,或者这是来自 firebase 的错误?

【问题讨论】:

    标签: firebase google-cloud-functions


    【解决方案1】:

    在客户端,您应该在初始化时指定所需区域并调用函数如下:

    var functions = firebase.app().functions('europe-west1');
    
    ....
    
    functions.httpsCallable("addRoom")(data)
    

    https://firebase.google.com/docs/functions/locations#http_and_client_callable_functions

    【讨论】:

      【解决方案2】:

      对于 Angular 应用程序,您应该在应用程序模块中提供这样的区域

      providers: [
          { provide: REGION, useValue: 'europe-west3' }
      ]
      

      然后像这样调用函数:

      constructor(private aff: AngularFireFunctions) {}
      
      sendMail(data: { ... }): Promise<any> {
          return this.aff.httpsCallable('function_name')(data).pipe(first()).toPromise()
      }
      

      【讨论】:

        猜你喜欢
        • 2018-10-16
        • 2020-10-24
        • 2021-03-17
        • 2021-08-15
        • 2020-11-29
        • 2021-09-15
        • 1970-01-01
        • 2020-09-08
        • 1970-01-01
        相关资源
        最近更新 更多