-2

I create a page for display members of the sport club using MERN stack. I will mention below that code. In the browser, display the error as

members.map is not a function

I want to solve that error

import React, { useEffect, useState } from "react";
import Button from "react-bootstrap/Button";
import "bootstrap/dist/css/bootstrap.min.css";
import "../../css/Admin.css";
import AdminNavbar from "./AdminNavbar";
import axios from "axios";

function MemberDashboard() {
  const [members, setMembers] = useState([]);
  const [isLoading, setLoading] = useState(false);

  useEffect(() => {
    const fetchData = async () => {
      try {
        const response = await axios.get("http://localhost:8000/posts");
        setMembers(response.data);
      } catch (error) {
        console.error("Error fetching members:", error.message);
      }
    };

    fetchData();
    function simulateNetworkRequest() {
      return new Promise((resolve) => setTimeout(resolve, 2000));
    }

    if (isLoading) {
      simulateNetworkRequest().then(() => {
        setLoading(false);
      });
    }
  }, [isLoading]);

  const handleActionClick = () => setLoading(true);

  return (
    <div>
      <div className="navshadow">
        <AdminNavbar />
      </div>
      <h1>Members of Sports Club</h1>
      <br />
      <table className="members">
        <thead>
          <tr>
            <th>No.</th>
            <th>Name</th>
            <th>Email</th>
            <th>Phone</th>
            <th>Action</th>
          </tr>
        </thead>
        <tbody>
          {members ? (
            members.map((member, index) => (
              <tr key={index}>
                <td>{index + 1}</td>
                <td>{`${member.fname} ${member.lname}`}</td>
                <td>{member.email || "-"}</td>
                <td>{member.phone || "-"}</td>
                <td>
                  <Button
                    variant="primary"
                    size="sm"
                    className="me-4"
                    disabled={isLoading}
                    onClick={!isLoading ? handleActionClick : null}
                  >
                    {isLoading ? "Loading…" : "Edit"}
                  </Button>

                  <Button
                    variant="danger"
                    size="sm"
                    disabled={isLoading}
                    onClick={!isLoading ? handleActionClick : null}
                  >
                    {isLoading ? "Loading…" : "DELETE"}
                  </Button>
                </td>
              </tr>
            ))
          ) : (
            <tr>
              <td colSpan="5">No members found</td>
            </tr>
          )}
        </tbody>
      </table>
    </div>
  );
}

export default MemberDashboard;

I want to solve above error and I want to know reason for that.

2
  • What data format does your /posts API return? You seem to be expecting an array but it is clearly not Commented Dec 20, 2023 at 23:24
  • There are hundreds of questions on Stack Overflow about exactly that error. There were 15000+ results from a search on your title Commented Dec 20, 2023 at 23:47

1 Answer 1

-2

You cannot map over objects in JS. You need to use Object.values() to get an array of values first.

 {members ? ( 
            Object.values(members).map((member, index) => (
            ...

You can also use Object.keys() to map over an array of keys, or Object.entries() to map over an array of (key, value) tuples.

Sign up to request clarification or add additional context in comments.

1 Comment

Please refrain from answering obvious duplicate questions and instead search for the duplicate and flag the question as such.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.