Firebase onCall 函数 return 4G 网络错误但适用于 Wifi

Firebase onCall function return Error with 4G network but works with Wifi

在我的 PWA 上,有一些功能 运行 通过 onCall 函数。它工作正常,但几天前,只有当 PWA 在具有 4g 网络的移动设备上 运行 时,这些功能中的任何一个才会 returning “Firebase 错误:内部”。在 Wifi 网络中,一切都按预期工作。

一些额外的信息:

  1. 在 4g 中,函数 return 出错,但在 firebase 控制台上没有任何消息记录。甚至显示“函数执行开始”或“函数执行已...”。
  2. 是的,我的 4g 没问题。尽管存在这个 onCall 函数错误,但其他需要连接的部分工作正常(例如:来自 SDK firebase 的任何数据库调用)
  3. 这不是我设备的特定问题。在某些设备上它可以工作,在其他设备上没有...我用 IOS 或 Android.
  4. 的不同设备进行了测试

我的代码 - 网页:

async checkFirstLogin(uid) {
    this.apiPortal = this.functions.httpsCallable('apiPortal')
    let body = {
      action: 'checkFirstLogin',       
      uid: uid
    }
    return this.apiPortal({body: body})
    .then((response) => {
      return response.data.success
    }).catch((e) => {
      console.log('========> Error', e) //returns "Firebase error: internal"
    })
}

我的代码 - 函数

const admin = require('firebase-admin');
const functions = require('firebase-functions');

try {
    admin.initializeApp();
} catch (e) {}

const runtimeOpts = {
    timeoutSeconds: 60,
    memory: '2GB'
}

exports.default = functions.region('southamerica-east1').runWith(runtimeOpts).https.onCall((data, context) => {
    if (!context.auth) {
        return {
            status: 'error', 
            code: 401, 
            message: 'Not signed in'
        }
    }
    functions.logger.log(`#apiPortal - starting - action:${data.body.action}`)
    if (data.body.action==='checkFirstLogin') {
        let ref = admin.database().ref(`login/${data.body.uid}`)
        return ref.once('value').then((data) => { 
          if (data.val()) {
            let usr = data.val()
            return {success: true, data: usr.hasOwnProperty('last_login')} 
          } else {
            return {success: true, data: null}
          }
        }).catch((e) => {
          throw new functions.https.HttpsError('Unknown error', e.message, e)
        })
    } else {
        return {success: false, data: null}
    }
  }
})

我们发现问题出在 TIM 4g 网络上。这是他们的 DNS 问题,没有解决我们在 GCP 中 API 的 url。今天,经过3天的问题,它再次工作

我面临着这个完全相同的问题,为此我找到的唯一解决方法是使用 firebase 托管为您的函数创建可重写的路径。这是一个解决方案,因为 firebase 托管不受 TIM 4g 网络这种极其烦人的行为的影响(在我的情况和@leandro-furini 的情况下)。

如何:

  1. 使用 firebase 托管发布一个网站(如果您已经有一个, 你可以使用它,否则你只能发布一个虚拟网站)。

  2. 在您的网站项目的 firebase.json 文件中,您需要像这样对您的函数放置重写指令:

{
   ...

   "rewrites": [
     {
        "source": "myFunctionPath", // the name you want to give to the path
        "function": "nameOfMyFunction" // the name of your function in the firebase
     },
   ]
}

并使用 firebase cli 部署您的网站:firebase deploy --only hosting

  1. 更改您的 firebase sdk(在我的例子中,我使用的是 AngularFire)以使用不同的来源,在这种情况下,来源必须是 firebase 托管网站地址,例如:https://my-app.web.app(这也适用于在 firebase 托管上添加的自定义域)。
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule.withServerTransition({ appId: 'serverApp' }),
    AppRoutingModule,
    LayoutModule,
    SwiperModule,
    AngularFireModule.initializeApp(environment.firebase),
    NgcCookieConsentModule.forRoot(cookieConfig),
    SweetAlert2Module.forRoot(),
    NgxMaskModule.forRoot()
  ],
  providers: [
    ScreenTrackingService, UserTrackingService, AngularFireAuthGuard,
    //{ provide: REGION, useValue: 'southamerica-east1' },
    //{ provide: USE_FUNCTIONS_EMULATOR, useValue: ['localhost', 5001] },
    { provide: LOCALE_ID, useValue: 'pt' },
    { provide: ORIGIN, useValue: 'https://my-app.web.app' } // ** HERE GOES YOUR HOSTIN ADDRESS **
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

此片段来自 Angular 和 AngularFire,但我 100% 确定可以使用本机 firebase sdk 配置可调用函数的来源。

  1. 确保您的云函数发布在 us-central1 区域(仅适用于该区域)。

就是这样!!现在当你从你的 pwa 中调用一个可调用函数时 地址将是:

https://my-app.web.app/myFunctionPath

并且会达到与调用

相同的结果

https://us-central1-my-project.cloudfunctions.net/nameOfMyFunction