Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

286
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda