0

having trouble trying to read an array I'm passing through a .then(), when I do I just get an error that the array is undefined, bit if I try to log it within the first .then() it shows correctly I was thinking maybe this kind of function would be best suited for an async/await function at this point..... but wouldn't that be the same as just adding a .then() but I'm not sure if there's a way I can resolve this without doing that with the logic :P

hoping for some insight, constructive criticism is welcome :)

    componentDidMount(){
        fetch(`https://api.nasa.gov/DONKI/CMEAnalysis?startDate=${this.state.time[0]}&endDate=${this.state.time[1]}&mostAccurateOnly=true&speed=500&halfAngle=30&catalog=ALL&api_key=${process.env.REACT_APP_APIKEY}`)
      .then(response => response.json())
      .then((data) => {   
            this.setState({coronaData:data})
            
            const dataRadar = {
                labels: ["speed", "halfAngle", "longitude", "latitude"],
                datasets: [
                    {
                    label: data[0].associatedCMEID,
                    backgroundColor: "rgba(194, 116, 161, 0.5)",
                    borderColor: "rgb(194, 116, 161)",
                    data:  [65, 59, 90, 81]
                    },
    
                    {
                    label: data[1].associatedCMEID,
                    backgroundColor: "rgba(71, 225, 167, 0.5)",
                    borderColor: "rgb(71, 225, 167)",
                    data:  [65, 59, 90, 81]
                    }
                ]
            }  
      })
      .then((dataRadar)=>{
          console.log(dataRadar)           //getting undefined when logging here
          this.setState({dataRadar:dataRadar})
      })
    }
luther wardle
  • 439
  • 5
  • 17

1 Answers1

0

You need to add return dataRadar in the second then, at the end, so that it's forwarded to the next then.

Akshay Kumar
  • 875
  • 13
  • 29