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

130
Visualizações
Using values from an array using useEffect giving multiple arrays

I'm trying to pull data from a database and compare the values from the database to the user logged in. The code I'm using to check this looks like:

  const [records, setRecords] = useState([]);
  const [email, SetEmail] = useState([]);

  // This method fetches the records from the database.
  useEffect(() => {
    async function getRecords() {
      const response = await fetch(`http://localhost:5000/admin/`);

      if (!response.ok) {
        const message = `An error occurred: ${response.statusText}`;
        window.alert(message);
        return;
      }

      const admin = await response.json();
      setRecords(admin);
      let emailList = [];
      admin.forEach((x) => {
        emailList.push(x.email);
        return emailList;
      }, SetEmail(emailList));
    }

    getRecords();

    return () => {

    };
  }, []);
  console.log(email);
  console.log(email.includes("xxx@yahoo.com"));

The problem with this is console.log(email) returns multiple empty arrays before returning the fully appended one making it so that if I compare the the current logged in email to the email in the database I get - false, false.. true. Making my component unrederable.

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

0

I needed to use the map() function instead

about 4 years ago · Juan Pablo Isaza Relatório

0

Since you are converting 1 array to another with the same number of entries you can use a map.

Treat console.log as side effects as well and place them inside of useEffects

  const [records, setRecords] = useState([]);
  const [email, SetEmail] = useState([]);

  // This method fetches the records from the database.
  useEffect(() => {
    async function getRecords() {
      const response = await fetch(`http://localhost:5000/admin/`);

      if (!response.ok) {
        const message = `An error occurred: ${response.statusText}`;
        window.alert(message);
        return;
      }

      const admin = await response.json();
      
      const emailList = admin.map(x => x.email);
      setRecords(admin);
      SetEmail(emailList));
    }

    getRecords();

    return () => {

    };
  }, []);

  useEffect(() => {
      console.log(email);
      console.log(email.includes("xxx@yahoo.com"));
  },[email]);
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