Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
104
rated 0 times [  108] [ 4]  / answers: 1 / hits: 23458  / 12 Years ago, wed, september 5, 2012, 12:00:00

I have this nodeJS code.



module.exports = {

foo: function(req, res){
...
this.bar(); // failing
bar(); // failing
...
},

bar: function(){
...
...
}
}


I need to call the bar() method from inside the foo() method. I tried this.bar() as well as bar(), but both fail saying TypeError: Object #<Object> has no method 'bar()'.



How can I call one method from the other?


More From » node.js

 Answers
9

You can do it this way:



module.exports = {

foo: function(req, res){

bar();

},
bar: bar
}

function bar() {
...
}


No closure is needed.


[#83242] Monday, September 3, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
darennevina

Total Points: 422
Total Questions: 128
Total Answers: 105

Location: Comoros
Member since Tue, Mar 14, 2023
1 Year ago
;