admin管理员组文章数量:1426036
I'm using keystone for my project. In 'Post' database there is publishedDate and I want to format it with code under.
function formatDate(date) {
var monthNames = [
"Ocak", "Şubat", "Mart",
"Nisan", "Mayıs", "Haziran", "Temmuz",
"Ağustos", "Eylül", "Ekim",
"Kasım", "Aralık"
];
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
return day + ' ' + monthNames[monthIndex] + ' ' + year;
}
var q = keystone.list('Post').model.findOne({
state: 'published',
slug: locals.filters.post,
}).populate('author categories');
q.exec(function (err, result) {
Object.keys(result).forEach(function (key) {
var element = result[key];
element.newDate = formatDate(element.publishedDate);
});
locals.data.post = result;
next(err);
});
});
When I run project an error occurs. But I know 'publishedDate' is a Date value.
Mongoose model 'error' event fired on 'Post' with error:
Cannot read property 'getDate' of undefined TypeError: Cannot read property 'getDate' of undefined
I'm using keystone for my project. In 'Post' database there is publishedDate and I want to format it with code under.
function formatDate(date) {
var monthNames = [
"Ocak", "Şubat", "Mart",
"Nisan", "Mayıs", "Haziran", "Temmuz",
"Ağustos", "Eylül", "Ekim",
"Kasım", "Aralık"
];
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
return day + ' ' + monthNames[monthIndex] + ' ' + year;
}
var q = keystone.list('Post').model.findOne({
state: 'published',
slug: locals.filters.post,
}).populate('author categories');
q.exec(function (err, result) {
Object.keys(result).forEach(function (key) {
var element = result[key];
element.newDate = formatDate(element.publishedDate);
});
locals.data.post = result;
next(err);
});
});
When I run project an error occurs. But I know 'publishedDate' is a Date value.
Mongoose model 'error' event fired on 'Post' with error:
Cannot read property 'getDate' of undefined TypeError: Cannot read property 'getDate' of undefined
Share
Improve this question
edited Aug 13, 2021 at 3:26
Molomby
6,6692 gold badges35 silver badges27 bronze badges
asked Jun 20, 2018 at 9:59
carbforcecarbforce
231 gold badge1 silver badge7 bronze badges
6
-
element.newDate = formatDate(new Date(element.publishedDate));
. you're passing a string to a function that expects a Date Object – Semi-Friends Commented Jun 20, 2018 at 10:00 -
It appears that
element.publishedDate
is not set. – Jonathon Reinhart Commented Jun 20, 2018 at 10:01 -
1
it says
property 'getDate' of undefined
, so you should try to find out why it isundefined
– Terry Wei Commented Jun 20, 2018 at 10:02 -
Just check if element has
publish edDate
property or not. If it doesn't have, it will beundefined
and you will get error as you're getting. – hygull Commented Jun 20, 2018 at 10:05 -
1
The whole
Object.keys(result).forEach
thing is suspect. You've donefindOne
. Would it really return an object where each and every property refers to an object with apublishedDate
property? That seems odd. I'd expect you to be doing afind
and getting an array. – T.J. Crowder Commented Jun 20, 2018 at 10:05
2 Answers
Reset to default 2Replace
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
Into
var day = new Date(date).getDate();
var monthIndex = new Date(date).getMonth();
var year = new Date(date).getFullYear();
From you code:
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
are you sure in the date
object you really have a getDate()
, date.getMonth()
, getFullYear()
because I haven't seen where you instantiated your date new Date()
.
Maybe you might want to go through this, it might help.
var today = new Date();
var day = today.getDate();
var month = today.getMonth()+1; //January is 0!
var year = today.getFullYear();
if(day<10) {
day = `0${day}`
}
if(month<10) {
month = `0${month}`
}
today = `${month} / ${day} / ${year}`;
alert(`Today's date is "${today}"`);
本文标签: javascriptCannot read property 39getDate39Stack Overflow
版权声明:本文标题:javascript - Cannot read property 'getDate' - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745465984a2659536.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论