Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: Cannot read property 'split' of undefined

Using JavaScript to split a date and rearrange the format.

Date is provided through a json feed as YYYY-MM-DD.

To get the date, I do:

var og_date = (v.report[totalItems -1].inspection_date); console.log(og_date); 

console log correctly shows the date, ie "2012-10-01".

Next, I try to split the date, for example:

console.log(og_date.value.split('-')); 

And I get:

Uncaught TypeError: Cannot read property 'split' of undefined  

Any ideas?

like image 842
Twitch Avatar asked Jun 13 '14 17:06

Twitch


People also ask

Can not read the property split of null?

The "Cannot read property 'split' of null" error occurs when the split() method is called on a variable that stores a null value. To resolve the error, make sure to only call the split() method on strings.

What is undefined in JavaScript?

A variable that has not been assigned a value is of type undefined . A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned .


1 Answers

Your question answers itself ;) If og_date contains the date, it's probably a string, so og_date.value is undefined.

Simply use og_date.split('-') instead of og_date.value.split('-')

like image 136
ThiefMaster Avatar answered Sep 23 '22 08:09

ThiefMaster