简体   繁体   English

ReferenceError:找不到变量:firebase。我无法将应用程序从 Expo 连接到 Firebase?

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

I am trying to create an App with a Database in which I will add several collections in Cloud Firestore.我正在尝试创建一个带有数据库的应用程序,我将在 Cloud Firestore 中添加几个 collections。 but it is impossible, since the app was broken when I added the code to connect the app.但这是不可能的,因为当我添加连接应用程序的代码时应用程序被破坏了。

I've seen various solutions on Stack and GitHub, but after hours of testing, it doesn't work.我在 Stack 和 GitHub 上看到了各种解决方案,但是经过几个小时的测试,它不起作用。

bud search 芽搜索

Firebase v9 modular - Can't find variable: IDBIndex Firebase v9 modular - 找不到变量:IDBIndex

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

For now the Application is very simple, only two files are involved in Firebase and nothing works目前应用非常简单,Firebase中只涉及两个文件,没有任何作用

I have changed the way to call Firebase in several ways:我已经通过几种方式更改了调用 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'

Currently the code I have is the following:目前我的代码如下:

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)

file actions.js文件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
}

And the errors that the console shows me:以及控制台向我显示的错误:

** **

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

** **

How can I correct this error?我该如何纠正这个错误?

You're importing functions from the newer modular SDK, but then are tryign to call the older namespaced APIs.您正在从较新的模块化 SDK 导入函数,但随后尝试调用较旧的命名空间 API。 That won't work.那行不通的。

I recommend using the code samples from getting started with Firebase Authentication on the web , getting the currently signed in user and the upgrade guide .我建议使用web 上的 Firebase 身份验证入门的代码示例, 获取当前登录的用户升级指南

With the new modular syntax, you can build a function that returns a promise with first auth state with:使用新的模块化语法,您可以构建一个 function,它返回一个 promise,第一个身份验证为 state,其中:

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)
    })
  });
}

If you want to stick to using the namespaced API, you can either import an older version of Firebase (eg the latest v8 release) or use the compat path for the new SDK, in the same upgrade guide I linked above.如果你想坚持使用命名空间 API,你可以导入旧版本的 Firebase(例如最新的 v8 版本)或使用新的 SDK 的兼容路径,在我上面链接的同一升级指南中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM