Jammming-search results return name and album "undefined"

Hi everyone, as I have completed all the steps up to 95 (except the steps for deployment) but the function didn’t work as expected.

The search results return undefined name and album!?
My source code:

From Spotify.js
async search(term) {
const accessToken = Spotify.getAccessToken();

    const response = await fetch(`https://api.spotify.com/v1/search?type=track&q=${this.term}`, {
        headers: { Authorization: `Bearer ${accessToken}` }
    });
    const jsonResponse = await response.json();
    if (!jsonResponse.tracks) {
        return [];
    }
    return jsonResponse.tracks.items.map(track => ({
        id: track.id,
        name: track.name,
        artist: track.artists[0].name,
        album: track.album.name,
        uri: track.uri
    }));
},

From SearchBar.js
export class SearchBar extends React.Component {
constructor(props) {
super(props);
this.state = {term: ‘’}

    this.search = this.search.bind(this);
    this.handleTermChange = this.handleTermChange.bind(this);
}

search() {
    this.props.onSearch(this.state.term)
}

handleTermChange(e) {
    this.setState({term: e.target.value})
}

render() {
    return (
        <div className="SearchBar">
            <input onChange={this.handleTermChange} placeholder="Enter A Song, Album, or Artist" />
            <button className="SearchButton" onClick={this.search}>SEARCH</button>
        </div>
    )
 }    

}

From App.js
class App extends React.Component {
constructor(props) {
super(props);

this.state = {
  searchResults: [],

  playlistName: 'My Playlist',

  playlistTrack: []
};
this.search = this.search.bind(this);

}
search(term) {
Spotify.search(term).then(searchResults => {
this.setState({searchResults: searchResults});
})
}
If anyone had encountered and solved the same issue please share your solution.
Thank you very much!

What have you tried so far to troubleshoot the issue?

Try using debugging techniques like looking in the browser developer tools console for error messages, use console.log() to check the value of variables being passed, and React chrome dev tools to check what props are passed between components and stored in state.

What is the value of the props term passed to the search() method, compared to the value of this.term used in the fetch call?

Did you ever solve this problem? I have the same issue.

I too am experiencing this issue. All of the other stuff works.

I’m thinking somehow the search term isn’t getting passed around properly.

I looked at my SearchBar.js file, and React Dev Tools shows that the state “term” in SearchBar.js indeed updates to the search term. But that same search term doesn’t seem to get passed to the code that actually displays the results for the search term. What gets passed instead is undefined which is a JS primitive data type.

My guess is this is a communication problem between SearchBar.js, App.js, and/or Spotify.js.

I do not understand React enough to know where the communication problem actually arises.

EDIT:

I fixed it–at least the issue that I am experiencing. The fix was entirely in SearchBar.js. The issue was an issue with a reference to the state object property “term”.

Looks like your code didn’t have the same issue my code had. “term” appears everywhere it should in your code.

Some troubleshooting for you

Maybe try making your fetch argument https://api.spotify.com/v1/search?type=track&q=${term} instead of https://api.spotify.com/v1/search?type=track&q=${this.term}?

Maybe make your .search() method NOT async?

The fix for my code is below.

// SearchBar.js - AFTER - THIS WORKS
import "./SearchBar.css";

import React from "react";

class SearchBar extends React.Component {
  constructor(props) {
    super(props);

    this.state = { term: "" }; 
    // Line 9 above - the "term" property of state object MUST match 
    // what is in Line 16 below

    this.search = this.search.bind(this);
    this.handleTermChange = this.handleTermChange.bind(this);
  }

  search() {
    this.props.onSearch(this.state.term); 
    // Line 16 above - Here, "term", is a reference to the "term" 
    // property of the state object
  }

  handleTermChange(e) {
    this.setState({ term: e.target.value });
  }

  render() {
    return (
      <div className="SearchBar">
        <input placeholder="Enter A Song, Album, or Artist" onChange={this.handleTermChange} />
        <button className="SearchButton" onClick={this.search}>
          SEARCH
        </button>
      </div>
    );
  }
}

export default SearchBar;

Compare the above to the below (the difference is subtle!)

// SearchBar.js - BEFORE - THIS DOES NOT WORK
import "./SearchBar.css";

import React from "react";

class SearchBar extends React.Component {
  constructor(props) {
    super(props);

    this.state = { term: "" }; 
    // Line 9 above - the "term" property of state object MUST match 
    // what is in Line 16 below

    this.search = this.search.bind(this);
    this.handleTermChange = this.handleTermChange.bind(this);
  }

  search() {
    this.props.onSearch(this.state.searchTerm); 
    // Line 16 above - Here, "searchTerm" does not actually 
    // reference anything
  }

  handleTermChange(e) {
    this.setState({ term: e.target.value });
  }

  render() {
    return (
      <div className="SearchBar">
        <input placeholder="Enter A Song, Album, or Artist" onChange={this.handleTermChange} />
        <button className="SearchButton" onClick={this.search}>
          SEARCH
        </button>
      </div>
    );
  }
}

export default SearchBar;

I was having an issue with the name of the songs coming up undefined and was trying to figure out if it was my json code or if it had to do with the term … and saw that the no matter what I typed into the search bar I was getting the exact same list of albums
but couldn’t figure out what was wrong with my SearchBar component.

This reply was super helpful and helped me see I had

instead of the appropriate

anyway, mostly just here to say thank you~!

Glad it worked.

Some issues I had in general with this project that you might share:

This project is way way way beyond the level the student is at in their learning process.

We are learning class components, which as I understand it are outdated. The thing to do now in React is to use function components. Which we later learn, but still.

This entire React course and the Jamming project might need a complete rewrite.

3 Likes

I agree. I do think it is good to learn what React was like as class components before and then move on to function components, in that it provides a better understanding of mounting, unmounting etc and if we encounter older code once we are working.
That being said, I’ve done some follow along React courses all with function components and also working on this Front-End path here in codeacademy so it was pretty confusing and annoying when I know that all of this class component stuff could be done so much simpler with function components.

Again, teaching the class components is fine but the entire Jamming project should be redone and saved for after learning the function components I think.

There have been a couple of projects that seemed to come out of left field during this path course and Jamming was definitely one of them and it can be really demotivating for newbies.

I’m working through the rest of the React now so don’t know what those projects are like… but hopefully better than this one. I haven’t even done the second half of it because I wanted to get to the function components.

2 Likes

Thank you so much! I was stuck on this for so long and realized i had this.props.term instead of this.state.term in the search method. Once I fixed that my undefined answers started populating

I agree, this project was originally a walkthrough project where you used React classes to build the main components. The Spotify API stuff was already handled for you. For them to turn around and turn it into a portfolio project was not a bad idea, but as you mentioned, it is way too advanced for the learner at this part in the course. The learner has not even learned hooks which is basically a requirement for the project. I went through the whole course before going back and doing the portfolio projects and so it was not as much as a problem for me since I knew where to look; However, I can see this project being very frustrating for people doing it along-side the normal flow of the course work.

5 Likes