Hi i am getting this error when i am trying to restructure the project as MVC. I am creating usecases in my models. So i am trying to get my structure like this: When a route is hit, it goes to the controller and then from the controller it looks up to the usecase.My index router file:
"use strict";
const express = require("express");
const router = express.Router();
const TodoTask = require("../models/entity/ToDoTask");
const {
ensureAuthenticated,
forwardAuthenticated,
} = require("../controller/authentication/index");
const { isAdmin } = require("../controller/authentication/adminAuthMiddleware");
const todos = require("../controller/todos/index");
const {
getAllTodos,
createTodo,
getTodo,
editTodo,
deleteTodo,
} = require("../controller/todos/index");
// Welcome Page
router.get("/", forwardAuthenticated, (req, res) => res.render("landing"));
router.get("/dashboard", ensureAuthenticated, getAllTodos);
router.get("/admin-dashboard", ensureAuthenticated, getAllTodos);
router.post("/dashboard", createTodo);
router.route("/edit/:id").get(getTodo).post(editTodo);
router.route("/remove/:id").get(deleteTodo);
router.get("/admin-dashboard", isAdmin, (req, res) =>
res.render("admin-dashboard")
);
module.exports = router;
I am creating a controller for getting all the todos
const TodoTask = require("../../models/entity/ToDoTask");
const TODOS = require("../../models/usecases/todo");
module.exports = {
getAllTodos: async () => {
try {
console.log("in the controller");
let todos = await TODOS.getAllTodos({});
return todos;
} catch (error) {
console.error(error);
}
},
and the error that i am getting is in the usecase
const TodoTask = require("../../models/entity/ToDoTask");
const getAllTodos = async function (req, res) {
if (req.user.admin === true) {
TodoTask.find({}, (err, tasks) => {
res.render("admin-dashboard.ejs", {
todoTasks: tasks,
user: req.user,
});
});
} else {
TodoTask.find({ "user.id": req.user._id }, (err, tasks) => {
res.render("todo.ejs", {
todoTasks: tasks,
user: req.user,
});
console.log(tasks);
});
}
};
so i am getting error as 'TypeError: Cannot read properties of undefined (reading 'admin') in models\usecases\todo.js Please help
Modify your function getAllTodos
with below code. If the req
object has user
object and in further it has admin
as true
, it should work as expected.
const TodoTask = require("../../models/entity/ToDoTask");
const TODOS = require("../../models/usecases/todo");
module.exports = {
getAllTodos: async (req, res) => {
try {
console.log("in the controller");
let todos = await TODOS.getAllTodos(req, res);
return todos;
} catch (error) {
console.error(error);
}
},
}
Edit: Modify your function from todo as below,
const TodoTask = require("../../models/entity/ToDoTask");
const getAllTodos = async function (req, res) {
if (req.user.admin === true) {
TodoTask.find({}, (err, tasks) => {
return res.render("admin-dashboard.ejs", {
todoTasks: tasks,
user: req.user,
});
});
} else {
TodoTask.find({ "user.id": req.user._id }, (err, tasks) => {
return res.render("todo.ejs", {
todoTasks: tasks,
user: req.user,
});
console.log(tasks);
});
}
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.