簡體   English   中英

無法讀取未定義的屬性(讀取“使用”)如何在 ExpressJS 中解決此問題?

[英]Cannot read properties of undefined (reading 'use') how can I resolve this issue in ExpressJS?

終端中的警告基於未定義(閱讀“使用”) - 在我的應用程序中引入的路由是 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 };

Team.js(類似於 Project.js、Auth.js、User.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);
  }
});

}

誰可能會面臨類似的警告:問題的根源在於沒有在 Server.js 的承包商中定義“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();
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM