0

The API does not render and it says this.state.weather.map is not a function. I need help, I have tried different ways of mapping through but does not change the outcome. Any feedback?

import React from 'react';
import axios from 'axios';
              
export default class Weather extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      weather: []
    }
  }
                
  async componentDidMount(){
    axios.get("http://api.weatherapi.com/v1/current.json?key=?=nyc").then(res => {
      console.log(res);
      this.setState ({weather: res.data})
    });
  }

  renderWeather(){
    this.state.weather.map(weather => {
      return (
        <p>{weather.location.name}</p>
      )
    })
  }

  render () {
    return (
      <div>
        {this.renderWeather()}
      </div>
    )
  }
}

2
  • 1
    You need to return your map from the function Commented Dec 25, 2020 at 15:20
  • I tried returning the map also and it still said the same error- Commented Dec 25, 2020 at 17:34

2 Answers 2

1
renderWeather(){
    return this.state.weather.map(weather => {
        return(
            <p>{weather.location.name}</p>
        )
    })
  }

you have missed the return statement inside the renderWeather function, above snippet works for you

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

6 Comments

I tried returning the map also and it still said the same error-- this.state.weather.map is not a function. I wonder what else could work ? thanks
check the API response, it supposed to return data in array format
Yup the API is logged in the console as array of objects, still not working.
Unhandled Rejection (TypeError): this.state.weather.map is not a function. That is the exact error message I am getting now.
console.log(this.state.weather); just before the return statement and check what is the format, it must be an array format
|
0

The API returns JSON.

this.setState ({weather: res.data})

Check the typeof res.data. Is is probably not an array but an object, hence this.state.weather.map is not a function, because Array.map() is an array method.

Make sure that you set the state properly to an array and it should work just fine..

4 Comments

Yes you are right. I just checked it returned Object, any idea on how to change it to an array? Thanks all.
You could use Object.values() to get an array of all values in the object. Check the documentation on MDN. Depending on what you want to do with your data you might also want to check out Object.keys() or Object.entries().
I changed my code to this.setState ([{weather: Object.entries(res.data)}]) and it still was not able to convert it fully to an array, I also tried object.key and object.values and it did not work still. This is the hardest API I have ever worked with lol .
However I was at least able to get it to look like this in the console - 0: (2) ["data", {…}]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.