无法在 class 中调用它在 Node.js 的 ES6 中定义的方法
Cannot call a method within a class it defined it in ES6 in Node.js
我正在使用 Node.js、Express.js 和 MongoDB 制作应用程序。
我正在使用 MVC 模式,并且还有单独的路由文件。
我正在尝试创建一个控制器 class,其中一个方法调用其中声明的另一个方法。但我似乎无法做到这一点。我得到 "Cannot read property '' of undefined".
index.js 文件
let express = require('express');
let app = express();
let productController = require('../controllers/ProductController');
app.post('/product', productController.create);
http.createServer(app).listen('3000');
ProductController.js 文件
class ProductController {
constructor(){}
create(){
console.log('Checking if the following logs:');
this.callme();
}
callme(){
console.log('yes');
}
}
module.exports = new ProductController();
当我 运行 时,我收到以下错误消息:
Cannot read property 'callme' of undefined
我自己有 运行 这段代码,稍作修改如下,它可以工作。
class ProductController {
constructor(){}
create(){
console.log('Checking if the following logs:');
this.callme();
}
callme(){
console.log('yes');
}
}
let product = new ProductController();
product.create();
为什么一个有效而另一个无效?
帮助!
当您将 create
方法作为方法传递时,它可能会像您预期的那样在不同的上下文 (this
) 中被调用。你可以绑定它:
app.post('/product', productController.create.bind(productController));
还有许多其他方法可以确保 this
引用正确的对象。
例如用函数(箭头或经典)包装它:
app.post('/product', (...args) => productController.create(...args));
或者在构造函数中绑定方法:
constructor() {
this.create = this.create.bind(this);
}
您的方法是 being rebound to the Layer
class within express,失去了它原来的上下文。 express 处理路由的方式是将每个路由包装在 Layer
class 中,它将路由回调分配给自己:
this.handle = fn;
这就是您的问题所在,此分配自动将函数上下文重新绑定到 Layer
。这是一个演示问题的简单示例:
function Example() {
this.message = "I have my own scope";
}
Example.prototype.logThis = function() {
console.log(this);
}
function ReassignedScope(logThisFn) {
this.message = "This is my scope now";
// simulation of what is happening within Express's Layer
this.logThis = logThisFn;
}
let example = new Example()
let scopeProblem = new ReassignedScope(example.logThis);
scopeProblem.logThis(); // This is my scope now
其他人已经指出了解决方案,就是将你的方法显式绑定到ProductController
实例:
app.post('/product', productController.create.bind(productController));
我正在使用 Node.js、Express.js 和 MongoDB 制作应用程序。 我正在使用 MVC 模式,并且还有单独的路由文件。 我正在尝试创建一个控制器 class,其中一个方法调用其中声明的另一个方法。但我似乎无法做到这一点。我得到 "Cannot read property '' of undefined".
index.js 文件
let express = require('express');
let app = express();
let productController = require('../controllers/ProductController');
app.post('/product', productController.create);
http.createServer(app).listen('3000');
ProductController.js 文件
class ProductController {
constructor(){}
create(){
console.log('Checking if the following logs:');
this.callme();
}
callme(){
console.log('yes');
}
}
module.exports = new ProductController();
当我 运行 时,我收到以下错误消息:
Cannot read property 'callme' of undefined
我自己有 运行 这段代码,稍作修改如下,它可以工作。
class ProductController {
constructor(){}
create(){
console.log('Checking if the following logs:');
this.callme();
}
callme(){
console.log('yes');
}
}
let product = new ProductController();
product.create();
为什么一个有效而另一个无效? 帮助!
当您将 create
方法作为方法传递时,它可能会像您预期的那样在不同的上下文 (this
) 中被调用。你可以绑定它:
app.post('/product', productController.create.bind(productController));
还有许多其他方法可以确保 this
引用正确的对象。
例如用函数(箭头或经典)包装它:
app.post('/product', (...args) => productController.create(...args));
或者在构造函数中绑定方法:
constructor() {
this.create = this.create.bind(this);
}
您的方法是 being rebound to the Layer
class within express,失去了它原来的上下文。 express 处理路由的方式是将每个路由包装在 Layer
class 中,它将路由回调分配给自己:
this.handle = fn;
这就是您的问题所在,此分配自动将函数上下文重新绑定到 Layer
。这是一个演示问题的简单示例:
function Example() {
this.message = "I have my own scope";
}
Example.prototype.logThis = function() {
console.log(this);
}
function ReassignedScope(logThisFn) {
this.message = "This is my scope now";
// simulation of what is happening within Express's Layer
this.logThis = logThisFn;
}
let example = new Example()
let scopeProblem = new ReassignedScope(example.logThis);
scopeProblem.logThis(); // This is my scope now
其他人已经指出了解决方案,就是将你的方法显式绑定到ProductController
实例:
app.post('/product', productController.create.bind(productController));