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

206
Visualizações
Async function unable to return a value

I'm an amateur at JS and was having this problem. I am making a chat app and I'm querying firebase firestore to check if a chat exists with a person with a particular email. Hence I am using a function call chatExists() and am passing in the email that I want to look up.

The problem is that the querying works fine and everything goes well, even the console.log() statements work but I cannot seem to get a value from the "return" statement. I want to return a boolean value, so that I can put it in an if statement and then take necessary actions based on whether or not the chat exists.

I tried looking at other problems at StackOverflow but I could not understand them. It would really mean a lot if anyone would tell me exactly what I'm doing wrong and exactly how I could fix this problem. If it's required to change a lot of the code I'd be really happy if someone would write that code out for me because this is a hobby project I'm building on my own and I can't really find any sort of help. Thanks in advance!

const chatExists = async (recipientEmail) => {
    try {
      const collectionRef = collection(db, "chats");
      const chatQuery = query(
        collectionRef,
        where("users", "array-contains", user.email)
      );

      const chatQuerySnapshot = await getDocs(chatQuery);
      chatQuerySnapshot.forEach((doc) => {
        if (doc.data().users.includes(recipientEmail)) {
          console.log("Contact exists");
          return true;
        } else {
          console.log("Contact does not exist");
          return false;
        }
      });
    } catch {
      (err) => {
        console.log(err);
      };
    }
  };
about 4 years ago · Juan Pablo Isaza
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