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.