So, I’m a little new to this… so bear with me. I was able to set up a route with a GET request that products aggregation pipeline results for me in JSON. It’s a pretty simple request that lists “review ratings” from highest to lowest. This is the code, and I have it in a viewController file:
exports.getHighestRatings = catchAsync(async (req, res, next) => {
const highestRatings = await Review.aggregate([
{
$sort: { rating: -1 },
},
]);
res.status(200).render('aggdisplay', {
title: 'Fun Stats!',
data: {
highestRatings,
},
});
console.log(highestRatings);
});
I’ll note that the console.log here displays the expected content. However, when I try to render anything from highestRatings in my Pug template file, it comes back as “undefined.” This is an example of the pug code:
extends base
block content
article#mainArticle
each rating in highestRatings.rating
span= ${highestRatings.rating}
It was brought to my attention by some crazy guy with a Viking hat that my problem may not be with MongoDB as much as it is with displaying data in JSON format on my front end. So, I’m guessing I should be searching how to properly format and render JSON in my pug template file. Can anyone confirm, provide me with thoughts / feedback? Thank you!