24 lines
805 B
JavaScript
24 lines
805 B
JavaScript
const Category = require("../models/category.js");
|
|
const Item = require("../models/item.js");
|
|
const asyncHandler = require("express-async-handler");
|
|
|
|
exports.index = asyncHandler(async (req, res, next) => {
|
|
// gets categories from database
|
|
const rawCats = await Category.find().lean().exec();
|
|
|
|
// function to get items for specified category from database
|
|
const getItems = async (category) => {
|
|
return await Item.find({ category: category._id }).lean().exec();
|
|
};
|
|
|
|
// creats a copy of the raw categories array with each category's items included as .items (copying is easier than overwriting)
|
|
const categories = [];
|
|
for (let rawCat of rawCats) {
|
|
rawCat.items = await getItems(rawCat);
|
|
categories.push(rawCat);
|
|
}
|
|
|
|
res.render("index", {
|
|
categories: categories,
|
|
});
|
|
});
|