const Message = require("../models/message.js"); const asyncHandler = require("express-async-handler"); exports.index = asyncHandler(async (req, res, next) => { // if user is logged in if (req.user) { // gets messages addressed to user from database const DMs = await Message.find({ to: req.user.username }).lean().exec(); // gets messages addressed to all chat from database const allChat = await Message.find({ to: "all" }).lean().exec(); // render index page return res.render("index", { DMs: DMs, allChat: allChat, user: req.user, }); } // otherwise redirect to login page res.redirect("/user/login"); });