Hello,
I am getting the error “Uncaught TypeError: Cannot read property ‘length’ of null” in the console and cannot figure out why this is happening. This code is populate a list of stores on a map in groups of 10 at a time and then when you click next it will only display 1 store and then when you click next again it will display 10 again. The error is that it should always display 10 stores at a time. The error is happening at the line
for (var i = 0; i < matches.length; i++) {
Any help is appreciated. Here is my javascript code.
formatStoreEvents: function(str) {
if (!str || str.length < 1) return '';
var today = moment();
// Format from MyFittingExp is:
// '<p>01/14/2017 12:00PM-03:00PM--<a href="https://my.taylormadegolf.com/myFittingExp/event/event_id">Book an Appointment</a><br/>02/25/2017 09:00AM-05:00PM--<a href="https://my.taylormadegolf.com/myFittingExp/event/event_id">Book an Appointment</a></p>'
var fittingEventRegEx = /<p>.*Book an Appointment<\/a><\/p>/g;
var matches = fittingEventRegEx.exec(str);
var storeEvents = str;
for (var i = 0; i < matches.length; i++) {
// clearing this string of fitting events from the original string
storeEvents = storeEvents.replace(matches[i], '');
var cleanStr = matches[i].replace('<p>', '').replace('</p>', '');
var fittngEventArray = cleanStr.split('<br/>');
var stringOfEvents = '';
for (var x = 0; x < fittngEventArray.length; x++) {
var eventDataDuplicate = fittngEventArray[x];
var eventData = fittngEventArray[x];
// get date and time string
eventData = eventData.substring(0, eventData.indexOf('--<'));
eventData = eventData.split(' ');
var eventDateAsMoment = moment(eventData[0], "MM/DD/YYYY");
if (today.isSame(eventDateAsMoment, 'day') || !today.isAfter(eventDateAsMoment)) {
stringOfEvents += '<br />' + eventDataDuplicate;
}
}
stringOfEvents = stringOfEvents.replace(/--/g, '<br>').replace(/<\/a>/g, '<br><\/a>');
stringOfEvents = stringOfEvents.replace(/href=/g, 'target="_blank" href=');
storeEvents = stringOfEvents + '<br />' + storeEvents;
}
return storeEvents;
}