Node JS call a "local" function within module.exports

sravis

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?

sravis

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to call a function from a chain of module.exports in node

From Dev

meaning of module.exports= function in node.js

From Dev

meaning of module.exports= function in node.js

From Dev

call a function within exports.run()

From Dev

How to call a function of a module dynamically in node js?

From Dev

module.exports = function() how to call

From Dev

Node.js Express module exports

From Java

module.exports vs exports in Node.js

From Dev

What is a context safe way to call functions within module.exports?

From Dev

Module exports and method call

From Dev

Node JS - How to call a methods inside a "module.export" function?

From Dev

Call a function from the same `module.exports` object by it name

From Dev

Defining routes within exports vs. above it Node.js

From Dev

How to write a typescript definition file for a node module that exports a function?

From Dev

Node module.exports reference own function like this keyword?

From Dev

Different results using Node.js' module.exports in Yeoman

From Java

Declare multiple module.exports in Node.js

From Java

Node.js - use of module.exports as a constructor

From Dev

How to reference a variable inside a module.exports in Node.js

From Dev

Different module.exports pattern in node.js

From Dev

Node.js module.exports not exporting the whole object

From Dev

Async readFile module.exports in node.js

From Dev

Node.js module.exports change principal variable

From Dev

Consolidate multiple callbacks in node.js module exports

From Dev

Node.js in module.exports my var is underfined

From Dev

How to use module.exports properly in node.js?

From Dev

How to call a function within the same module?

From Dev

Node.js: Making a call to asynchronous parent function from within asynchronous child function's callback

From Dev

module.exports function is not a function

Related Related

  1. 1

    How to call a function from a chain of module.exports in node

  2. 2

    meaning of module.exports= function in node.js

  3. 3

    meaning of module.exports= function in node.js

  4. 4

    call a function within exports.run()

  5. 5

    How to call a function of a module dynamically in node js?

  6. 6

    module.exports = function() how to call

  7. 7

    Node.js Express module exports

  8. 8

    module.exports vs exports in Node.js

  9. 9

    What is a context safe way to call functions within module.exports?

  10. 10

    Module exports and method call

  11. 11

    Node JS - How to call a methods inside a "module.export" function?

  12. 12

    Call a function from the same `module.exports` object by it name

  13. 13

    Defining routes within exports vs. above it Node.js

  14. 14

    How to write a typescript definition file for a node module that exports a function?

  15. 15

    Node module.exports reference own function like this keyword?

  16. 16

    Different results using Node.js' module.exports in Yeoman

  17. 17

    Declare multiple module.exports in Node.js

  18. 18

    Node.js - use of module.exports as a constructor

  19. 19

    How to reference a variable inside a module.exports in Node.js

  20. 20

    Different module.exports pattern in node.js

  21. 21

    Node.js module.exports not exporting the whole object

  22. 22

    Async readFile module.exports in node.js

  23. 23

    Node.js module.exports change principal variable

  24. 24

    Consolidate multiple callbacks in node.js module exports

  25. 25

    Node.js in module.exports my var is underfined

  26. 26

    How to use module.exports properly in node.js?

  27. 27

    How to call a function within the same module?

  28. 28

    Node.js: Making a call to asynchronous parent function from within asynchronous child function's callback

  29. 29

    module.exports function is not a function

HotTag

Archive