ReferenceError: Can't find variable: firebase . I can not connect the App to Firebase from Expo?

ReferenceError: Can't find variable: firebase . I can not connect the App to Firebase from Expo?

我正在尝试创建一个带有数据库的应用程序,我将在其中添加 Cloud Firestore 中的多个集合。 但这是不可能的,因为当我添加连接应用程序的代码时应用程序被破坏了。

我在 Stack 和 GitHub 上看到了各种解决方案,但经过数小时的测试,它不起作用。

bud search

Firebase v9 modular - Can't find variable: IDBIndex

https://github.com/expo/expo/issues/8089

目前应用程序非常简单,只有两个文件涉及 Firebase,没有任何效果

我已经改变了几种调用Firebase的方式:

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

import {initializeApp} from 'firebase/app'
import 'firebase/firestore'

import firebase from 'firebase/compat/app'
import 'firebase/compat/firestore'

目前我的代码如下:

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

const firebaseConfig = {
    apiKey:       "xxxxxxxxxxxxxxxx",
    authDomain:   "xxxxxxxxxxxxxxx",
    projectId:    "xxxxxxxxxxxxxxx",
    storageBucket: "xxxxxxxxxxxxxx",
    messagingSenderId: "xxxxxxxxxxx",
    appId:             "xxxxxxxxxxx"
}

export const firebaseApp = firebase.initializeApp(firebaseConfig)

文件actions.js

import { firebaseApp } from "./firebase"
import firebase from 'firebase/app'
import 'firebase/firestore'

const db = firebase.firestore(firebaseApp)

export const isUserLogged = () => {
  let isLogged = false
  firebase.auth().onAuthStateChanged((user)=> {
    user !== null && (isLogged = true)
  })
  return isLogged
}

以及控制台向我显示的错误:

**

TypeError: undefined is not an object (evaluating '_app.default.initializeApp')
- ... 9 more stack frames from framework internals
Invariant Violation: "main" has not been registered. This can happen if:
* Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project.
* A module failed to load due to an error and `AppRegistry.registerComponent` wasn't called.
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:104:6 in reportException
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:172:19 in handleException
at node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 in handleError
at node_modules/@react-native/polyfills/error-guard.js:49:36 in ErrorUtils.reportFatalError

**

如何更正此错误?

您正在从较新的模块化 SDK 导入函数,但随后尝试调用较旧的命名空间 APIs。那不行。

我建议使用 getting started with Firebase Authentication on the web, getting the currently signed in user and the upgrade guide 中的代码示例。

使用新的模块化语法,您可以构建一个函数,该函数 returns 具有第一个身份验证状态的承诺:

import { initializeApp } from "firebase/app";
import { getAuth, onAuthStateChanged } from "firebase/auth";

const firebaseConfig = {
  // ...
};

const app = initializeApp(firebaseConfig);
const auth = getAuth(app);

export const isUserLogged = () => {
  return new Promise((resolve, reject) => {
    let unsubcribe = onAuthStateChanged(auth, (user) => {
      unsubcribe();
      resolve(user !== null)
    })
  });
}

如果您想坚持使用命名空间 API,您可以导入旧版本的 Firebase(例如最新的 v8 版本)或 use the compat path 以导入新的 SDK,在相同的我在上面链接的升级指南。