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

203
Visualizações
TypeError: users.map is not a function

I was working on a react project which is using map function, i am using react router dom and redux also this error is struck when i fetch users from the mongoose database,the file which throws error is Users.js, and my frames collapse, the code is given below Users.js

import React, { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { fetchUsers } from '../../redux/actions/users/userActions';
import Loading from '../Loading/Loading';

const Users = () => {
  const dispatch = useDispatch();
  useEffect(() => {
    dispatch(fetchUsers());
  }, [dispatch]);
  const usersList = useSelector(state => state.usersList);
  const { loading, users, error } = usersList;

  console.log(users, loading, error);
  return (
    <div className='container-fluid'>
      <h1 className='text-center m-5'>List of users {users && users.length}</h1>
      <hr className='text-white' />
      <div className='row text-center justify-content-center'>
        {loading ? (
          <Loading />
        ) : (
          <>
            {users &&
              users.map(user => (
                <div className='col-lg-3' key={user._id}>
                  <div className='card'>
                    <div className='card-body'>
                      <h5 className='card-title'>{user.name}</h5>
                      <p className='card-text'>{user.email}</p>
                      <i className='far fa-address-card h2 text-info'></i>
                    </div>
                  </div>
                </div>
              ))}
          </>
        )}
      </div>
    </div>
  );
};

export default Users;

but while submitting response i am getting the following error

  20 | {loading ? (
  21 |   <Loading />
  22 | ) : (
> 23 |   <>
     | ^  24 |     {users &&
  25 |       users.map(user => (
  26 |         <div className='col-lg-3' key={user._id}>

can anyone help me sorting this?

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

0

Check your users object. May be its default value is defined but not with empty array (and you try to use map function), or users in selector have different (non-array) type after setting from server response.

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