Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!