Uncaught TypeError: Cannot read property 'split' of undefined

旧街凉风 提交于 2020-08-02 05:56:07

问题


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?


回答1:


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('-')




回答2:


ogdate is itself a string, why are you trying to access it's value property that it doesn't have ?

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

JSFiddle




回答3:


og_date = "2012-10-01";
console.log(og_date); // => "2012-10-01"

console.log(og_date.split('-')); // => [ '2012', '10', '01' ]

og_date.value would only work if the date were stored as a property on the og_date object. Such as: var og_date = {}; og_date.value="2012-10-01"; In that case, your original console.log would work.



来源:https://stackoverflow.com/questions/24210445/uncaught-typeerror-cannot-read-property-split-of-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!