[Solved] Javascript catch error


If the search on http://musicbrainz.org/ws/2/artist/? returns more than one artist, the object returns an array in

results.query.results.metadata["artist-list"].artist

So, to access the data, it would be

    quizCountry = results.query.results.metadata["artist-list"].artist[0].area.name;
    quizYear = results.query.results.metadata["artist-list"].artist[0]["life-span"].begin.match(/\d{4}/);
    quizBand = results.query.results.metadata["artist-list"].artist[0].name;

So, you’ll need to check if

results.query.results.metadata["artist-list"].count > 1

and change your code appropriately

e.g.

if(results.query.results.metadata["artist-list"].count > 1) {
    quizCountry = results.query.results.metadata["artist-list"].artist[0].area.name;
    quizYear = results.query.results.metadata["artist-list"].artist[0]["life-span"].begin.match(/\d{4}/);
    quizBand = results.query.results.metadata["artist-list"].artist[0].name;
} else {
    quizCountry = results.query.results.metadata["artist-list"].artist.area.name;
    quizYear = results.query.results.metadata["artist-list"].artist["life-span"].begin.match(/\d{4}/);
    quizBand = results.query.results.metadata["artist-list"].artist.name;
}

Although, you may want to change your logic totally if you get more than one artist in the response – but the above should fix your errors

solved Javascript catch error