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

279
Views
how to convert this into a async function?

I want to be able to retrieve the users from the Firestore database and filter to find a match between the id of the current logged in user with the id of the user from the database. I am not able to do that because I can't figure out a way to change this to async function:

const [loggedUser, setLoggedUser] = useState([]);
const [data, setData] = useState([]);

  useEffect(() => {
    const getUserData =  () => {
       onSnapshot(collection(db, "users"), (snapshot) => {
        let list = [];
          snapshot.docs.forEach((doc) => {
          list.push({ id: doc.id, ...doc.data() });
          setData(list);
        });

      }, (err) => {
        console.log(err);
      });

    }

    getUserData();


  }, [])

  useEffect(() => {

    const getLoggedUser = onAuthStateChanged(auth, (user) => {
      if (user) {
        const uid = user.uid;
        console.log(uid);
        if (data) {
          const signedUser = data.filter((item) => item.id === uid);
          setLoggedUser(signedUser);
        } else {
          console.log("no matching data")
        }
      } else {
        console.log("no user found")

      }
    });

    getLoggedUser();
  }, [])
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I want to be able to retrieve the users from the Firestore database and filter to find a match between the id of the current logged in user with the id of the user from the database.

You can use getDoc instead that'll only fetch the user's document and will cost you only 1 read. Currently you are reading the whole collection that'll cost you N reads where N is number of documents in the users collection.

You can use useEffect() just once and query Firestore when the auth state has been updated. Try refactoring the code as shown below:

import { getDoc, doc } from "firebase/firestore"

const [loggedUser, setLoggedUser] = useState([]);
const [data, setData] = useState([]);


useEffect(() => {
  onAuthStateChanged(auth, (user) => {
    if (user) {
      const uid = user.uid;
      console.log("User UID:", uid);
       
      const snapshot = await getDoc(doc(db, "users", uid));
    
      if (snapshot.exists) {
        setLoggedUser(snapshot.data());
      } else {
        console.log("user document missing")
      }
    } else {
      console.log("User not logged in")
    }
  });
}, [])
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!