我在节点 8 中有一个简单的类,但存在this
值问题。
module.exports = class Controller {
constructor() {
this.service = new Service();
}
create(request, response, next) {
try {
const body = request.body;
this.service.create(body)
console.log(this.service)// Undefined
} catch (error) {
next(error)
}
我遇到的问题是this
价值未定义。上面的控制器由路由器触发:
const Router = require("express").Router;
const Controller = require("./controller");
module.exports = class NewRouter {
constructor() {
this.router = new Router();
this.controller = new Controller();
this.initRoutes();
}
initRoutes() {
this.router.post("/setup", this.controller.create);
}
};