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

235
Vistas
ReactJS: map json and return to Table

I need to put the fetched data in a Table (it's a very simple CRUD)

I've tested putting the map() function outside of the Management() function but it doesn't work.

Here is my code :

let allUsers = [];

function Management() {
  useEffect(() => {
    const fetchData = async () => {
      try {
        const querySnapshot = await getDocs(collection(db, "users"));
        let allDocs = [];
        querySnapshot.forEach((doc) => {
          allDocs.push({ ...doc.data(), id: doc.id });
        });
        for (const item of allDocs) {
          const querySnap = await getDocs(
            collection(db, `users/${item.id}/general`)
          );
          allUsers.push(
            querySnap._snapshot.docChanges[0].doc.data.value.mapValue.fields
              .data.mapValue.fields
          );
        }
      } catch (err) {
        console.log(err);
      }
    };
    fetchData();
  }, []);

  return (
    <Table>
      <thead>
        <tr>
          <th>#</th>
          <th>Nom</th>
          <th>Prénom</th>
          <th>Email</th>
        </tr>
      </thead>
      <tbody>
        {allUsers.map((user) => {
          return (
            <tr>
              <th scope="row">1</th>
              <td>{user.email.stringValue}</td>
              <td>a</td>
              <td>@mdo</td>
            </tr>
          );
        })}
      </tbody>
    </Table>
  );
}

export default Management;

The problem occurs on the allUsers.map() function.

It seems that it returns an empty array

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are not changing the state of the component Since you are using a functional component, please use useState.

function Management() {
const [allUsers , setAllUsers] = React.useState<any>([]);
useEffect(() => {
const fetchData = async () => {
let allUsersData=[];
  try {
    const querySnapshot = await getDocs(collection(db, "users"));
    let allDocs = [];
    querySnapshot.forEach((doc) => {
      allDocs.push({ ...doc.data(), id: doc.id });
    });
    for (const item of allDocs) {
      const querySnap = await getDocs(
        collection(db, `users/${item.id}/general`)
      );
      allUsersData.push(
        querySnap._snapshot.docChanges[0].doc.data.value.mapValue.fields
          .data.mapValue.fields
      );
setAllUsers(allUsers);
    }
  } catch (err) {
    console.log(err);
  }
};
fetchData();
}, []);  return (
<Table>
  <thead>
    <tr>
      <th>#</th>
      <th>Nom</th>
      <th>Prénom</th>
      <th>Email</th>
    </tr>
  </thead>
  <tbody>
    {allUsers.map((user) => {
      return (
        <tr>
          <th scope="row">1</th>
          <td>{user.email.stringValue}</td>
          <td>a</td>
          <td>@mdo</td>
        </tr>
      );
    })}
  </tbody>
</Table>  );
}
export default Management;
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