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

142
Views
Getting posts from subcollection in users collection with collectionGroup using firebase javasscript SDK v9

What am I doing wrong here? posts state type returns undefined

Error:

TypeError: Cannot read properties of undefined (reading 'map')

Getting posts subcollection from firestore:

const [posts, setPosts] = useState([]);

useEffect(async () => {
  const querySnapshot = await getDocs(
    query(collectionGroup(db, "posts"), orderBy("createdAt", "desc"))
  );
  setPosts(querySnapshot.forEach((doc) => doc.data()));
}, []);

Inserting posts data:

<ScrollView>
  {posts.map((post, i) => (
    <Post post={post} key={i} />
  ))}
</ScrollView>;

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You directly can't make the useEffect hook as a async. For that you need to create a async method and call that method in useEffect hook.

Here is the sample code, that may help you

const [posts, setPosts] = useState([]);

useEffect(() => {
  const getData = async () => {
    const querySnapshot = await getDocs(
      query(collectionGroup(db, "posts"), orderBy("createdAt", "desc"))
    );
    setPosts(querySnapshot.forEach((doc) => doc.data()));
  };
  getData();
}, []);
...
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!