React Native firebase 未定义

React Native firebase is undefined

嗨,当我尝试 运行 我在 expo 上的 react-native 应用继续我的 phone

时,我一直收到这个错误

TypeError: undefined 不是一个对象(评估'_app.default.apps') 并且错误显示它指向下面代码中第 19 行的 firebase 变量,在 if 语句中它说 firebase:


import firebase from 'firebase/app';
require('firebase/auth');
import Constants from 'expo-constants';


// Initialize Firebase
const firebaseConfig = {
  apiKey: Constants.manifest.extra.apiKey,
  authDomain: Constants.manifest.extra.authDomain,
  projectId: Constants.manifest.extra.projectId,
  storageBucket: Constants.manifest.extra.storageBucket,
  messagingSenderId: Constants.manifest.extra.messagingSenderId,
  appId: Constants.manifest.extra.appId,
  measurementId: Constants.manifest.extra.measurementId
};

let Firebase;

if (firebase.apps.length === 0) {
  Firebase = firebase.initializeApp(firebaseConfig);
}

export default Firebase;

这反过来导致我相信也违反了不变量。

有什么帮助吗?

FirebaseSDK 在新版本 9 中更改了它们的 API,所以现在 import firebase from “firebase/app” 将无法使用

您需要使用 import { initializeApp } from 'firebase/app'; 启动您的应用程序或从 firebase/compat/app 导入以使用旧的 API

参考https://firebase.google.com/docs/web/modular-upgrade for more about the changes and https://firebase.google.com/docs/web/setup新的API