0

I'm fetching data using axios as the following :

{
    "data": {
        "3": {
            "id": 4,
            "value": "FaAlgolia",
            "name": "zaefzaef",
            "profile_id": 4,
            "order": 9000,
            "created_at": "2021-05-17T20:25:03.000000Z",
            "updated_at": "2021-05-17T20:25:03.000000Z",
            "url": "katon",
            "profile": {
                "id": 4,
                "username": "xxxxxxxxx",
                "bio": "zfzefzef",
                "preview": "ssss",
                "address": "zefzef",
                "country": "us",
                "p_color": "#ffffff",
                "s_color": null,
                "logo": "afffable",
                "showlogo": "null",
                "selectedOption": "option2",
                "url": "katon",
                "user_id": 2,
                "created_at": null,
                "updated_at": "2021-05-17T20:28:08.000000Z"
            }
        },
        "4": {
            "id": 5,
            "value": "FaAlipay",
            "name": "azdadzzad",
            "profile_id": 4,
            "order": 9000,
            "created_at": "2021-05-17T20:41:53.000000Z",
            "updated_at": "2021-05-17T20:41:53.000000Z",
            "url": "katon",
            "profile": {
                "id": 4,
                "username": "xxxxxxxxx",
                "bio": "zfzefzef",
                "preview": "ssss",
                "address": "zefzef",
                "country": "us",
                "p_color": "#ffffff",
                "s_color": null,
                "logo": "afffable",
                "showlogo": "null",
                "selectedOption": "option2",
                "url": "katon",
                "user_id": 2,
                "created_at": null,
                "updated_at": "2021-05-17T20:28:08.000000Z"
            }
        }
    }
}

In React :

  const [list, setList] = React.useState([]); 

  useEffect(() => {
    axios
      .get("http://localhost:8000/api/social/profile/katon")
      .then((response) => {
       let test = (response.data.data)
        setList(test)
      })
      .catch((err) => console.log(err));
  }, []);

console.log of test enter image description here

I know that I have a problem with the received array, I tried adding [] but none of that worked.

I searched React code throwing “TypeError: this.props.data.map is not a function” & React JS - Uncaught TypeError: this.props.data.map is not a function and didn't find my answer.

AlyaKra
  • 486
  • 8
  • 22

1 Answers1

1

Your response.data.data is an object and objects don't have .map functions. You will need to convert it to an Array.

If you only want the values of the data object and have no use of the keys (3,4) then you can use

let test = Object.values(response.data.data);
Abdullah Razzaki
  • 972
  • 8
  • 16