Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
156
rated 0 times [  157] [ 1]  / answers: 1 / hits: 26932  / 9 Years ago, mon, october 12, 2015, 12:00:00

How do you call a function from within another function in a module.exports declaration?



I have MVC structure node js project and a controller called TestController.js. I want to access method within controller, but using this keyword gives below error:




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
}
}


How do I access methods within controller?


More From » node.js

 Answers
41

I found the solution :-)



use strict
var self = module.exports = {
myName : function(req, res, next) {
// accessing method within controller
self.getName(data);
},

getName : function(data) {
// code
}
}

[#64772] Thursday, October 8, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rickjordond

Total Points: 100
Total Questions: 105
Total Answers: 90

Location: Sweden
Member since Mon, May 8, 2023
1 Year ago
;