mongodb 无法读取未定义的属性(阅读“use”)如何在ExpressJS中解决此问题?

nukf8bse  于 2023-01-20  发布在  Go
关注(0)|答案(1)|浏览(121)

终端中的警告基于未定义(阅读'use')-在我的应用程序中引入的路由是app〉router〉routers.js

const express = require("express");
const app = express();
const router = require("express").Router();
const { authRouters } = require("./Auth");
const { projectRouters } = require("./Project");
const { teamRouters } = require("./Team");
const { userRouters } = require("./User");
router.use("/project", projectRouters);
router.use("/team", teamRouters);
router.use("/user", userRouters);
router.use("/Auth", authRouters);
module.exports = { AllRouters: router };

团队. js(类似于项目. js、授权. js、用户. js)

const router = require("express").Router();

> my codes...

module.exports = { projectRouters: router };

最后在我的Server.js中

createRoutes() {
this.#app.get("/", (req, res, next) => {
  return res.json({ message: "Welcome to my app" });
});
this.#app.use((error, req, res, next) => {
  try {
    this.#app.use(AllRouters);
  } catch (error) {
    next(error);
  }
});

}

des4xlb0

des4xlb01#

他们也会面临类似的警告:问题的根源在于Server.js的contractor中没有定义“AllRouters”方法,解决方案为:

module.exports = class Application {
  #app = express();
  #PORT;
  #DB_URL;
  #AllRouters;
  constructor(PORT, DB_URL, AllRouters) {
    this.#PORT = PORT;
    this.#DB_URL = DB_URL;
    this.#AllRouters = AllRouters;
    this.ConfigApplication();
    this.ConnectToMongoDb();
    this.CreateServer();
    this.CreateRoutes();
    this.ErrorHandler();
  }

相关问题