Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

131
Vistas
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 Respuestas
Responde la pregunta

0

I needed to use the map() function instead

about 4 years ago · Juan Pablo Isaza Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda