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

121
Visualizações
Have a null Async function with react-native firebase and database

I have a lot trouble with async functions and assign to a variable on react-native.

So to put in context, I wanna retrieve a data from a database (firebase https://rnfirebase.io/database/usage), so I called a function database(), but I always get null. I realized that this function is asynchronous and I want to get sequentially the data from top to bottom. So I tested it out by printing my code and I got "2" before "1".

What I want is to print the data (from the database), but I get a null

I did many search on google and stackOverflow and I'm struggling on getting this data from asynchronous function.

So how I can I change my code to be able to print the value from the database

const [data, setData] = useState();

useEffect(() => {
  database()
    .ref(`/${id}`)
    .once('value')
    .then(snapshot => {
      setData(snapshot);
      console.log(snapshot);       //I can see data that I want to retrieve
      console.log(1);
    });
}, []);

console.log(2);
console.log(data) // I WANT TO PRINT HERE WITH THE VALUE expected the data from firebase-database, but have null as value 
about 4 years ago · Juan Pablo Isaza
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