Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Fetching Data with Axios

Erik L
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Erik L
Full Stack JavaScript Techdegree Graduate 19,470 Points

why do we need an extra data?

I noticed that when requested data with fetch we only wrote data after responseData once only

componentDidMount() {
    //start fetching data from gifs api using FETCH METHOD
    fetch('http://api.giphy.com/v1/gifs/trending?api_key=dc6zaTOxFJmzC')
      .then(response => response.json()) //this function takes the response from fetch and turns it to json format
      .then(responseData => { //make JSON data available to app by setting state
        this.setState({
          gifs: responseData.data }); //to access data in array use .data
      })
      .catch(error => { //use catch method to handle errors
        console.log('Error fetching and parsing data', error);
      });  
  }

but when we used axios to request data we had to write response.data.data

componentDidMount() {
    axios.get('http://api.giphy.com/v1/gifs/trending?api_key=dc6zaTOxFJmzC') 
    .then(response => { 
      this.setState({
        gifs: response.data.data
      });
    }) 
    .catch(error => { 
      console.log('Error fetching and parsing data', error);
   });  
  }

could someone please tell me why we wrote data.data in axios but not in fetch???

1 Answer

Ernesto Salazar
Ernesto Salazar
21,555 Points

Hello, thats because axios returns an object itselfs contaning many properties, one of them is called "data" which is an object that contains the response from the giphy API which also have an array property called "data"