Home > other >  Why does firebase.initializeApp not work?
Why does firebase.initializeApp not work?

Time:12-25

I've basically copy and pasted the exact code from the firebase documentation on how to set up firestore. When I try to run my, app, I get a breaking error, which says my app has not been initialized.

here is my firebase.js config file:

import { initializeApp } from "firebase/app";
import { fbConfig } from "./secrets.js"
import { getFirestore } from "firebase/firestore";

const firebaseApp = initializeApp({
  apiKey: fbConfig.apiKey,
  authDomain: fbConfig.authDomain,
  projectId: fbConfig.projectId,
  storageBucket: fbConfig.storageBucket,
  messagingSenderId: fbConfig.messagingSenderId,
  appId: fbConfig.appId
});

console.log(firebaseApp)
export default getFirestore()

For comparison, here is a link to the firebase docs. My console log is returning this:

FirebaseAppImpl {_isDeleted: false, _options: {…}, _config: {…}, _name: '[DEFAULT]', _automaticDataCollectionEnabled: false, …}
{...}
[[Prototype]]: Object

Here is the code where firebase is being called, in a different file:

import { useState, useEffect } from "react";
import { useForm } from "react-hook-form";
import { doc, setDoc } from "firebase/firestore";
import db from "../firebase" 
//I'm expecting db to be my exported firestore call from firebase.js

function Admin() {
  const onFormSubmit = async (data) => {
    await setDoc(doc(db, "interviewees", data.name), {
      name: data.name,
      location: data.location,
      age: data.age,
    });
  };

  return(<a big component>)
}

When the Admin component renders, this error appears:

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

so, it looks to my like there is an app being initialized. But my app crashing and the error message seems to disagree with my assessment. I'd appreciate any help!

CodePudding user response:

Try to return db not called function like export default getFirestore()

const firebaseApp = initializeApp({...});

const db = getFirestore() // In this line function are assigning database to a variable.

export { db }

Example of usage db:

import { addDoc, collection } from 'firebase/firestore'
import { db } from '...../firebase.js' // change path to your file

function addDocument(path, data) {
   addDoc(collection(db, path), data)
}

CodePudding user response:

Looks like I just had to delete my node modules and package log and npm install...

  • Related