Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

283
Vistas
Cannot access 'db' before initialization - Firebase

I'm getting a problem where my React app is crashing due to not being able to access db.

I am initialising the firebase app in src/index.js

import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { BrowserRouter } from "react-router-dom";
import { initializeApp } from "firebase/app";
import { firebaseConfig } from "./firebase";
import { getFirestore } from "firebase/firestore";

initializeApp(firebaseConfig);
export const db = getFirestore();

ReactDOM.render(
  <React.StrictMode>
    <BrowserRouter>
      <App />
    </BrowserRouter>
  </React.StrictMode>,
  document.getElementById("root")
);

Then in a different file thats in src/dashboard/signup/SignupForm.js I try to update db to add a registered user when user submits a validated form ( the only way I have been able to figure out how to list all registered users is through db since there is no direct feature that lets you do that ).

import { db } from "../../..";

// const db = getFirestore()

const unsub = onSnapshot(doc(db, "users", "userCount"), (doc) => {
  console.log("Current data: ", doc.data());
});

Here I'm trying to sub to live changes so that if a few users register at the same time the value updates correctly.

However in both ways, if I set db in src/index.js or in the form folder my app crashes.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Ok,

The problem was that I didn't move the db initialisation into the return of component

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda