Express with ES6 class 'this' 未定义
Express with ES6 class 'this' is undefined
你好我正在尝试使用 express 和 es6 classes 创建一个应用程序,但是每次调用 class 方法时 this
是未定义的。
UserController.js
export class UserController {
constructor() {
console.log('Class Constructed');
this.service = new UserService();
}
create(req, res, next) {
console.log('console', this);
this.service.create(req.body).then((user) => {
res.send(user);
});
}
}
route.js
import { Router } from 'express';
const router = Router();
router.route('/user/create')
.post(new UserController().create);
我注意到当我执行 npm run dev
时,我在控制器构造函数中获得了控制台,但是当我从邮递员调用“/user/create”时。我得到了 TypeError: Cannot read property 'service' of undefined
.
我错过了什么吗?或者这种方法可行吗?
如果有人能帮助我,那就太好了。
非常感谢。
你必须存储最近创建的UserControoller
实例,然后调用它的create
函数,所以this
是在场景下推断的。
router.route(`/user/create`, (req, res, next) => {
const controller = new UserController();
controller.create(req, res, next);
});
甚至调用 create
函数而不存储实例。
router.route(`/user/create`, (req, res, next) => {
new UserController().create(req, res, next);
});
通过传递 create
函数而不调用自己,该函数将在某个时刻被 express
调用,并且 express
将无法绑定 this
属性 的 prototype
,因为它没有引用实例。
希望对您有所帮助。
也许你需要 bind
create
功能 class
constructor() {
console.log('Class Constructed');
this.service = new UserService();
this.create = this.create.bind(this);
}
示例
class UserService {
create(data) {
return Promise.resolve({})
}
}
class UserController {
constructor() {
console.log('Class Constructed');
this.service = new UserService();
this.create = this.create.bind(this);
}
create(req, res, next) {
console.log('console', this);
this.service.create(req.body).then((user) => {
res.send(user);
});
}
}
function post(callback) {
callback({
body: {}
}, {
send: () => {}
}, () => {})
}
post(new UserController().create);
你好我正在尝试使用 express 和 es6 classes 创建一个应用程序,但是每次调用 class 方法时 this
是未定义的。
UserController.js
export class UserController {
constructor() {
console.log('Class Constructed');
this.service = new UserService();
}
create(req, res, next) {
console.log('console', this);
this.service.create(req.body).then((user) => {
res.send(user);
});
}
}
route.js
import { Router } from 'express';
const router = Router();
router.route('/user/create')
.post(new UserController().create);
我注意到当我执行 npm run dev
时,我在控制器构造函数中获得了控制台,但是当我从邮递员调用“/user/create”时。我得到了 TypeError: Cannot read property 'service' of undefined
.
我错过了什么吗?或者这种方法可行吗?
如果有人能帮助我,那就太好了。
非常感谢。
你必须存储最近创建的UserControoller
实例,然后调用它的create
函数,所以this
是在场景下推断的。
router.route(`/user/create`, (req, res, next) => {
const controller = new UserController();
controller.create(req, res, next);
});
甚至调用 create
函数而不存储实例。
router.route(`/user/create`, (req, res, next) => {
new UserController().create(req, res, next);
});
通过传递 create
函数而不调用自己,该函数将在某个时刻被 express
调用,并且 express
将无法绑定 this
属性 的 prototype
,因为它没有引用实例。
希望对您有所帮助。
也许你需要 bind
create
功能 class
constructor() {
console.log('Class Constructed');
this.service = new UserService();
this.create = this.create.bind(this);
}
示例
class UserService {
create(data) {
return Promise.resolve({})
}
}
class UserController {
constructor() {
console.log('Class Constructed');
this.service = new UserService();
this.create = this.create.bind(this);
}
create(req, res, next) {
console.log('console', this);
this.service.create(req.body).then((user) => {
res.send(user);
});
}
}
function post(callback) {
callback({
body: {}
}, {
send: () => {}
}, () => {})
}
post(new UserController().create);