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

310
Visualizações
Map through firebase firestore collectiongroup, Not working

const [theposts, setThePosts] = useState([]);

const myPosts = query(collectionGroup(db, 'posts'));
const unsub = onSnapshot(myPosts, (querySnapshot) => {
  querySnapshot.forEach((doc) => setThePosts({
    post: doc.data()
  }))
  return unsub;
})

const Posts = theposts.post;

return (
<>
  <StatusBar barStyle = "dark-content" backgroundColor = {'#b300b3'} />
  <SafeAreaView style={[styles.container, {marginTop: 5}]}>
  <Header navigation = {navigation} />
  <Stories />
  <ScrollView>
  {Posts.map((post, index) => (
  <Post post={post} key = {index} />
  ))}
  </ScrollView> 
    { /* <BottomTabs icons={bottomTabsIcons} /> */ }
  </SafeAreaView> 
  </>
)

It seems like there's no better way to write this, but still, it either gives an error(1): undefined is not a function (near'...Post.map') OR error(2): theposts.postes is not a function. theposts.postes is undefined

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Fetching posts from Cloud Firestore is a side effect and should be done inside of a useEffect hook.


async function getPosts() {
  const query = query(collectionGroup(db, "posts"));

  const querySnapshot = await getDocs(query);
  let posts = [];
  querySnapshot.forEach((doc) => {
    posts.push(doc.data());
  });

  setPosts(posts);
}

useEffect(() => {
  // Fetch post documents when component mounts
  getPosts();
}, []);


Then refactor rendered JSX as below

return (
<>
  <StatusBar barStyle = "dark-content" backgroundColor = {'#b300b3'} />
  <SafeAreaView style={[styles.container, {marginTop: 5}]}>
  <Header navigation = {navigation} />
  <Stories />
  <ScrollView>
  {posts.map((post, index) => (
  <Post post={post} key = {index} />
  ))}
  </ScrollView> 
  </SafeAreaView> 
  </>
)

about 4 years ago · Juan Pablo Isaza Relatório
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