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

128
Visualizações
.map() is not defined as a function

I am trying to use ".map()" to map documents which I'm getting from firebase. However it says .map() is not a function. Code is below. Basically I'm trying to fetch my firebase data, then render each doc as a element.

 <div{posts.map((doc) => <p>{doc.data().text}</p>)}</div>

How I decleared useState posts variable:

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

How I set useState posts variable:

useEffect(() => {
  db.collection('posts').onSnapshot((querySnapshot) => {
    setPosts(querySnapshot);

  });
})

Also, I tried using ".forEach". I can "console.log" the each documents by using ".forEach" but ".forEach" doesn't allow to return elements.

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

0

first log your querySnapshot and see whether it's an array, then you should check if posts' array is not null. try this :

<div{posts.length > 0 ? posts.map((doc) => <p>{doc.data().text}</p>) : null}</div>
about 4 years ago · Juan Pablo Isaza Relatório

0

.forEach can be used over Sets, Maps and Arrays, whereas .map can be used only over arrays. So, it is likely that the querySnapshot is returning a set that is updated on posts, since posts is now a Set, map is unable to iterate and return values from the iterable posts. To overcome this, you could convert the Set to an array before updating the state.

useEffect(() => {
  db.collection('posts').onSnapshot((querySnapshot) => {
    let querySnapshotToArray = Array.from(querySnapshot)
    setPosts(querySnapshotToArray);

  });
})

Now that the post is an array, you could map over it and return the required values.

about 4 years ago · Juan Pablo Isaza Relatório

0

when react component render the first time posts is null or undefined so .map was undefined you need to use Optional chaining, represented by ?. in JavaScript, which is a new feature introduced in ES2020.

 <div{posts?.map((doc) => <p>{doc?.data()?.text}</p>)}</div>
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