firebase 无法访问Cloud Firestore后端,连接失败1次

fjnneemd  于 2022-11-30  发布在  其他
关注(0)|答案(5)|浏览(286)

我用一个非常简单的代码从firestore获取数据

import firebase from 'firebase/app';
import 'firebase/firestore'

const firebaseApp = firebase.initializeApp({
        apiKey: "...",
        authDomain: "...",
        ....
    });
    
const db = firebaseApp.firestore();
    
export default db;

但我一直收到这个错误

[2021-06-05T00:58:41.274Z]  @firebase/firestore: Firestore (8.6.5): Could not reach Cloud Firestore backend. Connection failed 1 times.
Most recent error: FirebaseError: [code=permission-denied]:
 Permission denied on resource project.
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.

1.我有一个非常快的网络连接
1.我的时钟也与标准时间同步
我不知道为什么会这样?
请有人帮帮我!2!3!

mspsb9vt

mspsb9vt1#

我也遇到了同样的问题,连接在某些环境下可以正常工作,但在我客户的公司网络上却不行。
在互联网上做了很长时间的研究后,我发现an issue on Github在谈论它。
以下是对我有效的方法:

const firestoreDB = initializeFirestore(firebaseApp, {
  experimentalForceLongPolling: true, // this line
  useFetchStreams: false, // and this line
})

这里我使用的是firebase v9。对于firebase v8,它类似于:

firebase().firestore().settings({
  experimentalForceLongPolling: true, // this line
  useFetchStreams: false, // and this line
})
kadbb459

kadbb4592#

我能够通过直接使用我的凭据来解决这个问题,在那里我初始化了我的firebase配置,我以前是从我的env文件调用它们的,我认为我的应用程序之前没有获得env

2hh7jdfx

2hh7jdfx3#

确保您的环境指向真正的Firestone数据库,而不是Firestore仿真器。当我遇到同样的问题时,这就是原因。
我使用的是Angular框架,所以我必须在app.module.ts文件中注解掉这些环境引用。

@NgModule({
  declarations: [AppComponent, InformUserComponent],
  entryComponents: [InformUserComponent],
  imports: [
    BrowserModule,
    IonicModule.forRoot(),
    AngularFireModule.initializeApp(environment.firebaseConfig),
    AppRoutingModule,
    ServiceWorkerModule.register('ngsw-worker.js', {
      enabled: environment.production,
      // Register the ServiceWorker as soon as the app is stable
      // or after 30 seconds (whichever comes first).
      registrationStrategy: 'registerWhenStable:30000'
    }),

  ],
  providers: [AuthService, { provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
    // {
    //   provide: USE_FIRESTORE_EMULATOR, useValue: environment.useEmulators ?
    //     ['localhost', 8080] : undefined
    // },
    // {
    //   provide: USE_FUNCTIONS_EMULATOR, useValue: environment.useEmulators ?
    //     ['localhost', 5001] : undefined
    // },
  ],
  bootstrap: [AppComponent],
})
export class AppModule { }

u1ehiz5o

u1ehiz5o4#

我在使用angular fire时也遇到过这个问题。我所做的改变是将firestore设置添加到应用程序模块的提供者中:
根据GitHub issues的讨论,我认为你可以先试试experimentalAutoDetectLongPolling。除了使用非常旧的浏览器版本的用户之外,useFetchStreams选项是不必要的。

import { SETTINGS as FIRESTORE_SETTINGS } from '@angular/fire/firestore';

providers:[
    {
      provide: FIRESTORE_SETTINGS,
      useValue: { experimentalAutoDetectLongPolling: true, merge: true },
    }
]

并且可以使用mitmproxy来重现firebase错误。

to94eoyn

to94eoyn5#

我遇到了同样的问题,并尝试使用@atunde arisekola方法。如果您可以通过在firebaseConfig变量中使用直接凭证来解决这个问题,请考虑检查您使用的.env.local或任何其他.env是否位于根目录中。在我的例子中,firebaseConfig.ts.env.local位于同一目录中。因此发生错误。建议在应用程序的根目录中设置.env

相关问题