admin管理员组

文章数量:1429781

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 is undefined – 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 be undefined 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 done findOne. Would it really return an object where each and every property refers to an object with a publishedDate property? That seems odd. I'd expect you to be doing a find and getting an array. – T.J. Crowder Commented Jun 20, 2018 at 10:05
 |  Show 1 more ment

2 Answers 2

Reset to default 2

Replace

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