Bug Report: TypeError: languages[0] is undefined
-
Hi!
I have a small bug to report:
Movies with no language throw a JS
TypeError: languages[0] is undefined
which stops the rendering of the Overview tab entirely.THE DETAILS:
The movie The Artist throws the above error and here’s why.
The error itself is thrown by line 66 of file wp-content/plugins/my-movie-database/mmdb_templates/components/content/movie/movie-overview.js:
return languages[0].name + '(' + original + ')'
On lines 61–65 you do a sweet job of regenerating a new array of spoken languages where the iso_639_1 code matches that of the original_language.
Now in normal situations, this works fine, however, in the case of The Artist things get interesting because the movie returns the following spoken language:
english_name: "No Language" ??iso_639_1: "xx" ??name: "No Language"
On the other hand, it returns
en
as the original_language.As such, since the two do not match, the new regenerated array ends up being an empty array. So when line 66 tries to run, it has no data to work with and thus throws the error.
THE FIX:
This can easily be fixed by wrapping line 66 with one more check for whether the array has data.if (languages.length) { return languages[0].name + '(' + original + ')' }
While we’re here, I’d even recommend adding a space before the opening parenthesis to create nicer formatting like
English (en)
rather thanEnglish(en)
.Having said that, the Original Movie Language is a highly confusing field that can cause a lot of issues depending on how a movie was tagged. In The Artist example, because it’s tagged as
en
but that’s not a spoken_language, your code would end up runningreturn original
on line 70 and returnen
. So I’d like to suggest that it may make more sense to try to use the first spoken_language.name rather than the original, as this would outputNo Language
instead.
- The topic ‘Bug Report: TypeError: languages[0] is undefined’ is closed to new replies.