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

181
Visualizações
Cannot destructure property 'chat' of '_ref' as it is undefined (firebase & react)

I have this code that runs to fetch data from a firebase collection:

useEffect(() => {
db.collection("chatLists")
.onSnapshot((snapshot) => {
  if(snapshot) {
    let chattingLists = snapshot.docs.map((doc) => {
      if(doc.data().users.includes(user.email)) {
        return {
          chat: doc.data(),
          id: doc.id
        };
      }
    })
    if(chattingLists !== [undefined]) {
      setChatLists(chattingLists ? chattingLists : []);
    }
  }
})}, []);

Basically I am fetching all the docs of collection 'chatLists' and seeing if an array called "users" which has two users includes the current users name who's using the app, if they do, I return them all and all the filtered docs get inputed inside the chattingLists variable. Then I set it to a useState and then I try to map all of them:

return (
<div>
  <button onClick={addChat}>
    Add a chat
  </button>
  <>
    {chatLists !== undefined && chatLists.map(({chat, id}) => (
      <p>{id}</p> {/*Anything that i try to get from the chat or id doesn't work and i get error message*/}
    ))}
  </>
</div>

)

However, I get the error: "Cannot destructure property 'chat' of '_ref' as it is undefined."

And the weird part is that in some chrome users it works perfectly how I want it to and no errors but in some others I get that error message above.

Also, each doc in the chatLists looks like this:enter image description here

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

0

  1. You can use array-contains query operator instead of fetching all documents from the collection and manually filtering the documents. This will save you plenty of read charges.

  2. You must return a value from the function in map but currently you return nothing when the document does not contain given email and hence there are undefined items in that array.

Try refactoring the code as shown below:

useEffect(() => {
  db.collection("chatLists")
    .where("users", "array-contains", "user.email")
    .onSnapshot((querySnapshot) => {
      const chats = querySnapshot.docs.map((d) => ({
        id: d.id,
        chat: d.data(),
      }));
      setChatLists(chats);
    });
}, []);
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