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

111
Views
Why the object returned from a fetch function is a function outside it?

I don't get why data type is an object inside my function but outside is a function... Any ideas why ? Thanks.

const fetchAllUsersData = async () => {
    setLoading(true);
    const userFromFirebase = await firebase.database().ref("users").orderByChild("nickname").equalTo(nickname).on("child_added", ((snapshot) => {
        const data = snapshot.val();
        console.log("data", typeof data); // show object
        return data;
    }));
    console.log("userFromFirebase",typeof userFromFirebase); // show function
}

fetchAllUsersData();
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Read the documentation: https://firebase.google.com/docs/reference/node/firebase.database.Reference#on

The .on method will return anything, not your DataSnapshot. A better approach to this would be to use callbacks instead. Here is an example:

const fetchAllUsersData = async (callback) => {
    setLoading(true);
    const userFromFirebase = await firebase.database().ref("users").orderByChild("nickname").equalTo(nickname).on("child_added", ((snapshot) => {
        const data = snapshot.val();
        console.log("data", typeof data); // show object
        callback(data);
    }));
}

fetchAllUsersData((data) => {
        console.log("data", typeof data); // still shows object
});
about 4 years ago · Juan Pablo Isaza Report
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!