Leff
Leff

Reputation: 1370

Node - Using the function that is exported in the same file

I am trying to use the function that I am exporting in the same file, but I get undefined error:

 $(document).ready(function(){
     $.get('https://apiEndpoint.com)
        .done(function(data) {
          for (var key in data.subscriptionsInSet) {
            userSubscriptions.push(data.subscriptionsInSet[key].productName);
          }

          myFunction();

      });
  });

module.exports.myFunction = function() {
  console.log(userSubscriptions);
};

How can I use the function that I am exporting, in that same file?

Upvotes: 1

Views: 53

Answers (1)

Denys Séguret
Denys Séguret

Reputation: 382514

You have two simple solutions:

1) using the complete path when accessing your function:

module.exports.myFunction();

2) declaring your function also as a local variable:

var myFunction = module.exports.myFunction = function(){

When your code grows a little unclear with those declarations, you can also write a purely local code followed by a unique export:

module.exports = {
    myFunction1,
    myFunction2,
};

Upvotes: 2

Related Questions