Lieutenant Dan
Lieutenant Dan

Reputation: 8284

instances of 'Uncaught TypeError: Cannot read property 'includes' of null'

For some reason my if statement here, occasionally throws the below error and I can't figure out why:

Uncaught TypeError: Cannot read property 'includes' of null

Here; I also tried rewriting it in reverse and it still throws on '.includes', but then sometimes it works perfectly without error?

if (['en','or', 'de', 'ur', 'bn', 'ch'].some(original_language => item.original_language.includes(original_language))) {
    // content happens
}

More relevant code:

jQuery.ajax({   
                url: "https://www.aworkingapi.com",
                type: "GET",
    },
    success: function(data){                    

              $.each(data.content, function (index, item) {
                    if (['en','or', 'de', 'ur', 'bn', 'ch'].some(original_language => item.original_language.includes(original_language))) {
                    html += "<div class="customContent">";
                    html += "<div class='original_language'><span class='b'>Lan:</span> " + item.original_language + "</div>";

                    // more content
                    },
                    dataType: 'json'    
                  }
                })  

Upvotes: 0

Views: 1632

Answers (1)

hoangdv
hoangdv

Reputation: 16147

Your .original_language maybe null in some item of data.content, you can print out data.content via console.log, you can see item(s) without .original_language.

I sugest you need check .original_language is exist or not ( and it is a array).

if (['en','or', 'de', 'ur', 'bn', 'ch'].some(original_language => item.original_language && item.original_language.includes(original_language))) {
    // content happens
}

Upvotes: 2

Related Questions