[英]Cannot read properties of undefined (reading 'use') how can I resolve this issue in ExpressJS?
The warning in the terminal is based on undefined (reading 'use')-introduced route in my app is app>router>routers.js
终端中的警告基于未定义(阅读“使用”) - 在我的应用程序中引入的路由是 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 (similarly for Project.js, Auth.js, User.js)
Team.js(类似于 Project.js、Auth.js、User.js)
const router = require("express").Router();
> my codes...
module.exports = { projectRouters: router };
and finally in my Server.js
最后在我的 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);
}
});
} }
To whom may face a similar warning: the root of the problem is a matter of not defining " AllRouters" method in the contractor of the Server.js and the solution is:
谁可能会面临类似的警告:问题的根源在于没有在 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.