0

I'm working on a Codecademy project, Jammming, creating a playlist maker with the Spotify API. I've searched everything online, other repos, videos, and I still keep getting the mapping error. I'm not sure what's up? Any ideas?

Posted on github here: https://github.com/carriepresley/jammming

import React from "react";
import "./TrackList.css";
import Track from "../Track/Track";




class TrackList extends React.Component {
    render(){
        return (
            <div className="TrackList">
            {this.props.tracks.map((track) =>{
                    return <Track 
                    key = {track.id}
                    track = {track}
                    />
                })
            }
            </div>
        )
    }
}
export default TrackList;

3 Answers 3

1

This is a really common error that you'll almost certainly encounter again, but the reason for it varies. It is usually caused by data that is loaded asynchronously, meaning at the beginning the data is empty (in your case tracks) and later on it gets filled with an array, but there are other causes too which I will explain later.

There are several methods to make sure this doesn't break your app.

The quick and dirty way is to just check that the data is truthy before accessing properties: data && data.map. I only recommend doing this if there is no better way available, it can sometimes hide an error instead of fix it.

Personally I think the best practice is to make sure your variables are defaulted to empty structures representing what the data will look like. It looks like you already do this in your App.js by defaulting both searchResults and playlistTracks to be arrays in state.

The reason I prefer this, is that now if you still get an error, you know the problem is not an async issue, but something else. Such as forgetting to pass a prop a few levels up (App.js):

<SearchResults searchResults = {this.state.searchResults}/>
//<Playlist/> 
<Playlist tracks={this.state.playlistTracks} /> // Pass it the prop it needs

Playlist attempts to give TrackList the prop tracks from this.props.tracks, but it was never given that prop from App.js so it is undefined.

<TrackList tracks = {this.props.tracks}/> // this.props.tracks is undefined from App

This is why you have the error "Cannot read property .map of undefined". Simply checking if this.props.tracks is not undefined will stop the error, but will hide the bug!

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

1 Comment

Thank you Brian!! This explanation is incredibly helpful!
1

try :

  <div className="TrackList">
        {this.props.tracks && this.props.tracks.map((track) =>{
                return <Track 
                key = {track.id}
                track = {track}
                />
            })
        }
        </div>

1 Comment

It WORKED! Thank you so much Krimo!
0

For the Problem Solution: Track 34 generate an Error to Who follow the video walkthrough Cause: Track 32 is repeated 2 times in the video so the Track 33 is missing inside the video walkthrough

Solution:

Inside the App.js Find

<Playlist/>

change in

<Playlist searchResults={this.state.searchResults}/>

Inside Playlist.js find

<TrackList />

change in

<TrackList tracks={this.props.searchResults}/>

Comments

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.