Home > Enterprise >  TypeError: Cannot read properties of undefined (reading 'map') , Unable to get data from a
TypeError: Cannot read properties of undefined (reading 'map') , Unable to get data from a

Time:02-04

const handleData = (e) => {
  e.preventDefault()
  var config = {
    method: 'get',
    maxBodyLength: Infinity,
    url : ''
    headers : {}
  };
  axios(config)
    .then((response) => {
      setIsData(JSON.stringify(response.data))
      console.log(response);
    })
    .catch((error)=> {
      console.log(error);
    });  
}  
       
<p onClick={(e) => handleData(e)}>
  {isData?.map(product => {
    return <p key={sectionTypes.NEWLY_JOINED}>{product}</p>
  })}
</p>

I've been trying to display id's to sort and display them according to their respective section ids, but this error keeps on coming on the page.

the console.log says "GET_TODAY_PROFILE_FAILURE"

I don't know what I am doing wrong. Is it something in the map function I am doing wrong or am I doing something wrong while getting the data?

CodePudding user response:

You are stringify your response in state and then directly using map function on the state for that reason this error occurs.

you need to parse your data and then apply map function on it.

possible solutions:

  1. don't stringify the response so you can directly use it as a array form.
  2. parse your state before using them with JSON.parse(isData) in some variable and then add map function to this array variable.
  • Related