docwho
docwho

Reputation: 73

Uncaught TypeError: Cannot read property 'substr' of undefined

Pardon me for not being clear, but have thia script that is really long. When I have it live, I get this error in Chrome's Console.

Uncaught TypeError: Cannot read property 'substr' of undefined

here is the snippet of code where it is reading from.

var formIddd = $('select[class~="FormField"]').get(numSelec).name.substr($('select[class~="FormField"]').get(numSelec).name.length-3,2);

I looked up substr on google and it appears to be a known property. I also found the classes. I have played with the lengths, but still getting stuck. It used to work until BigCommerce did an update.

Any advice much appreciated, cheers.

Upvotes: 2

Views: 64100

Answers (3)

Ahmedakhtar11
Ahmedakhtar11

Reputation: 1458

Instead of:

variable.substring(0, 7)

Do:

variable && variable.substring(0, 7)

This is how you check for nullity like Basheer mentioned

Upvotes: 0

Basheer AL-MOMANI
Basheer AL-MOMANI

Reputation: 15327

maybe at some point the substr() is called on a null reference

so before using it check that reference for nullity like that

function(jsonDate) {

     if (jsonDate!=null) {
        //if the variable is not null you can use substr with no problems
         var date = new Date(parseInt(jsonDate.substr(6)));
        //.....
}

take look at my full snippet from Datatable

 columns: [
                        { 'data': 'ID' },
                        { 'data': 'Name' },
                        {
                            'data': 'DateCreated',
                            'render': function(jsonDate) {
                                if (jsonDate!=null) {
                                    var date = new Date(parseInt(jsonDate.substr(6)));
                                    return date.toLocaleDateString();
                                }
                                return "-";
                            }
                        },

Upvotes: 0

epascarello
epascarello

Reputation: 207521

You are not populating your array. The if check is false.

enter image description here

so basically you are doing this

var arrayOfSelectOfCountry = [];
var numSelec = arrayOfSelectOfCountry[-1];  //undefined

which results in the error above.

Upvotes: 2

Related Questions