1

Essentially I have a input field and a submit button. My goal is to have the input field be what is requested in the axios request.

This is the code for my axios request:

import axios from 'axios';

module.exports = {
  getItunesArtists: function(ARTIST_NAME) {
    var encodedURI = window.encodeURI('https://itunes.apple.com/search?term=' + ARTIST_NAME);

    return axios.get(encodedURI).then(function(response) {
      console.log('response.data.items', response.data.items);
    });
  },
};

The console spits back

response.data.items undefined

And this is my main component and a functional component.

import React, { Component } from 'react';
import api from '../utils/api';

import axios from 'axios';

// prettier-ignore
function Albums(props) {
  return (
    <ul>
      {props.albums.map(function(album, index) {
        return (
          <li key={album.name} >

            <ul >
              <li>
               {console.log(album)}
              </li>

            </ul>
          </li>
        );
      })}
    </ul>
  );
}

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = { albums: [], artistSearched: '', dirty: false };
    this.handleSearchTermChange = this.handleSearchTermChange.bind(this);
    this.getAlbums = this.getAlbums.bind(this);
  }

  componentDidMount() {
    this.getAlbums(this.state.artistSearched);
  }

  getAlbums(artist) {
    this.setState(function() {
      return {
        artistSearched: artist,
        albums: [],
      };
    });
    api.getItunesArtists(artist).then(
      function(artists) {
        this.setState(function() {
          return {
            albums: artists,
          };
        });
      }.bind(this)
    );
  }

  handleSearchTermChange(event) {
    console.log(event.target.value);
    this.setState({ artistSearched: event.target.value });
  }

  render() {
    const { albums, artistSearched } = this.state;

    return (
      <div>
        <h1>Itunes Album Fetcher</h1>
        <form style={{ marginTop: '20px' }}>
          <input
            onChange={this.handleSearchTermChange}
            value={this.state.artistSearched}
            className="form-control"
            placeholder="Enter album name"
          />
          <button type="submit" disabled={!artistSearched}>
            Search
          </button>
        </form>
        {!this.state.albums ? <p>Loading</p> : <Albums albums={this.state.albums} />}
      </div>
    );
  }
}

Thank you in advance!

1
  • 1
    Did you try console.log(JSON.stringify(response)) (or look at the network tab) to see what the response consists of? Commented Mar 20, 2018 at 22:36

1 Answer 1

1

You're console.logging response.data.items, but when I call this API I see the format of the response is different:

{
 "resultCount":50,
 "results": [ ... ]
}

You want to use response.data.results

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

2 Comments

Thanks, but that returns an empty array.
Then your search resulted in no results

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.