Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase error: No firebase app default has been created

I am making a food delivery app using react-native and redux. I want to fetch the data from the firebase store and for that, I have written a function in the actions.js, but whenever I run the app it shows the Error

Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app).

Here is the function which I am using to fetch the data action.js

import firebase from "firebase"
export const ProductDetails = (data) => {
  return {
    type: "PRODUCT_ITEMS",
    payload: {
      data,
    },
  };
};

var db = firebase.firestore();
var docRef = db.collection("Products").doc("Items");
export const FetchItems = () => {
  return async function (dispatch) {
    return await docRef
      .get()
      .then((doc) => {
        if (doc.exists) {
          console.log("Document Data", doc.data());
          dispatch(ProductDetails(doc.data));
        } else {
          console.log("NO such document");
        }
      })
      .catch((error) => {
        console.log(error);
      });
  };
};

Here is my App.js file

import React, { useState } from "react";
import { StyleSheet, Text, View, Dimensions } from "react-native";
import { NavigationContainer } from "@react-navigation/native";
import AppStack from "./components/navigation/AppStack";

import firebase from "firebase";
import {Provider} from "redux"
import store from "./store"

import { useDispatch, useSelector, Provider } from "react-redux";
import store from "./redux/store";
import AppWrapper from "./AppWrapper";

export default function App() {
  
  const firebaseConfig = {
  };
  if (firebase.apps.length === 0) {
    firebase.initializeApp(firebaseConfig);
  }
  return  (
    <Provider store={store}>
      <NavigationContainer>
        <AppStack />
      </NavigationContainer>
    </Provider>
  );;
}


like image 209
Ayush Kumar Avatar asked Jun 08 '26 13:06

Ayush Kumar


1 Answers

I would recommend creating a separate file firebase.js and export Firebase services from there after initialization.

firebase.js

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

const firebaseConfig = {...};

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

export const auth = firebase.auth();
export const firestore = firebase.firestore()

Then import these wherever you need them:

// App.js for example

import {firestore, auth} from './firebase.js'

//var docRef = firestore.collection("Products").doc("Items");

like image 77
Dharmaraj Avatar answered Jun 10 '26 04:06

Dharmaraj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!