edi_90
edi_90

Reputation: 25

Uncaught: TypeError

I'm trying put iTunes search to my app, and I met a problem with filter method, I fetched iTunes api to Array, and now I wanna filter that Array by title of song. I'm not understand one thing, why console display:

Uncaught TypeError: songs.filter is not a function
    at findSongs (script.js:10)
    at <anonymous>:1:1<br><br>

const endpoint = 'https://itunes.apple.com/search?term=jack+johnson&entity=song';
let songs = [];
fetch(endpoint)
    .then(blob => blob.json())
    .then(data => songs = data);
function findSongs(wordToMatch, songs){
    return songs.filter(title => {
        const regex = new RegExp(wordToMatch, 'gi');
        return title.trackName.match(regex)
    });
};

I'm struggling with this problem 2 hours and I got stuck.
any ideas??
thank you very much

Upvotes: 0

Views: 56

Answers (1)

Ritwick Dey
Ritwick Dey

Reputation: 19012

The endpoint returns an object which holds all songs details (as an array) in results property.

E.g. {count : 2, results: [ {}, {} ]}


Try .then(data => songs = data.results);

const endpoint = 'https://itunes.apple.com/search?term=jack+johnson&entity=song';
let songs = [];
fetch(endpoint)
    .then(blob => blob.json())
    .then(data => songs = data.results);

Upvotes: 3

Related Questions