如何在 module.exports 声明中从另一个函数中调用一个函数?
我有 MVC 结构 Node js 项目和一个名为 TestController.js 的 Controller 。我想在 Controller 中访问方法,但是使用 this
关键字会出现以下错误:
cannot call method getName of undefined
"use strict"
module.exports = {
myName : function(req, res, next) {
// accessing method within controller
this.getName(data);
},
getName : function(data) {
// code
}
}
如何访问 Controller 中的方法?
最佳答案
我找到了解决方案:-)
"use strict"
var self = module.exports = {
myName : function(req, res, next) {
// accessing method within controller
self.getName(data);
},
getName : function(data) {
// code
}
}
关于javascript - Node JS 在 module.exports 中调用一个 "local"函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33078967/