简体   繁体   English

错误:无法读取未定义的属性(读取“管理员”)

[英]Error: Cannot read properties of undefined (reading 'admin')

Hi i am getting this error when i am trying to restructure the project as MVC.您好,当我尝试将项目重组为 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:所以我试图让我的结构是这样的:当一条路线被击中时,它会到达 controller,然后从 controller 它查找到用例。我的索引路由器文件:

"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我正在创建一个 controller 来获取所有待办事项

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所以我收到错误为'TypeError:无法读取模型\用例\ todo.js中未定义的属性(读取'admin')请帮助

Modify your function getAllTodos with below code.使用以下代码修改您的 function getAllTodos If the req object has user object and in further it has admin as true , it should work as expected.如果req object 有user object 并且进一步它有admin作为true ,它应该按预期工作。

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,编辑:从 todo 修改您的 function,如下所示,

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);
    });
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:无法读取未定义的属性(读取“get”) - Error: Cannot read properties of undefined (reading 'get') 错误:无法读取未定义的属性(读取“主机”) - Error: Cannot read properties of undefined (reading 'host') 错误无法读取未定义的属性(读取“配置”)。 TypeError:无法读取未定义的属性(读取“配置”) - ERROR Cannot read properties of undefined (reading 'configurations'). TypeError: Cannot read properties of undefined (reading 'configurations') TypeError:无法读取未定义(读取“过滤器”)和未定义错误的属性 - React - TypeError: Cannot read properties of undefined (reading 'filter') and undefined error - React 未定义错误:TypeError:无法读取未定义的属性(读取“图标”) - Undefined Error: TypeError: Cannot read properties of undefined (reading 'Icon') 无法读取未定义的属性(读取 'then') - Cannot read properties of undefined (reading 'then') 无法读取未定义的属性(读取“4”) - Cannot read properties of undefined (reading '4') 无法读取未定义的属性(读取“______”) - Cannot read properties of undefined (reading '______') 无法读取未定义的属性(读取“打开”) - Cannot read properties of undefined (reading 'on') Next.js 错误:无法读取未定义的属性(读取“集合”) - Next.js Error: Cannot read properties of undefined (reading 'collection')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM