React Native :Firebase错误:未创建Firebase应用程序[默认]-调用Firebase应用程序,initializeApp()(应用程序/无应用程序)

tf7tbtn2  于 2022-11-17  发布在  React
关注(0)|答案(2)|浏览(222)

当我尝试将Firebase与我的React原生应用程序连接时,我遇到了此错误。
Firebase错误:未创建Firebase应用程序[默认]-调用Firebase应用程序。initializeApp()(应用程序/无应用程序)
我在这里添加了我的代码-

import { initializeApp } from "firebase/app";
    import { getAuth } from "firebase/auth";
    import { getFirestore } from "firebase/firestore";

   // Your web app's Firebase configuration
const RNfirebaseConfig = {
  apiKey: "........",
  authDomain: "note-app-rn.firebaseapp.com",
  projectId: "note-app-rn",
  storageBucket: "note-app-rn.appspot.com",
  messagingSenderId: ".....",
  appId: "......"
};

const app = initializeApp(RNfirebaseConfig);
export const auth = getAuth(app);
export const db = getFirestore(app);
0dxa2lsx

0dxa2lsx1#

试试这个,

import firebase from '@react-native-firebase/app';
import '@react-native-firebase/auth';
import '@react-native-firebase/firestore';

const RNfirebaseConfig = {
  apiKey: "........",
  authDomain: "note-app-rn.firebaseapp.com",
  projectId: "note-app-rn",
  storageBucket: "note-app-rn.appspot.com",
  messagingSenderId: ".....",
  appId: "......"
};

let app;
if (firebase.apps.length === 0) {
    app = firebase.initializeApp(RNfirebaseConfig )
} else {
    app = firebase.app()
}

const db = firebase.firestore();
const auth = firebase.auth();
gojuced7

gojuced72#

错误的主要来源与初始化配置设置有关。
checkout 此用法;

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

export const firebaseConfig = {
    apiKey: "............",
    authDomain: "............",
    projectId: "............",
    storageBucket: "............",
    messagingSenderId: "............",
    appId: "............",
    measurementId: "............"
}

if (!firebase.apps.length){
    firebase.initializeApp(firebaseConfig);
}

相关问题