Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140101

Node.js: how to deal with getFullYear in pug

$
0
0

I am trying to get the lifeSpan by subtracting dateOfBirth from dateOfDeath. getFullYear() is returning the year in console. But, in pug.. it is throwing error as follows: Cannot read property 'getFullYear' of undefined

Author Schema for lifeSpan:

AuthorSchema
.virtual('lifeSpan')
.get(function() {
    const lifeSpan = (this.dateOfDeath.getFullYear() - this.dateOfBirth.getFullYear()).toString();
    return lifeSpan;
});

Author Controller:

exports.authorList = async(req, res, next) => {
    try {
        await Author.find({}).exec((error, authorList) => {
            if(error) return authorList;

            // console.log(authorList);

            res.render('./author/index', { title: 'Author List', authorList: authorList});
        });
    } catch (error) {
        res.status(500).json({ message: error.message });
    }
};

Index.pug:

ul
    each author in authorList
      li 
        a(href=author.url) #{author.name}
        |  (#{author.lifeSpan})

    else
      li There is No Author in The List.

Any help would be appreciated.


Viewing all articles
Browse latest Browse all 140101

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>