Tuesday 9 May 2017

Javascript - importing and exporting variables and functions [duplicate]

This question already has an answer here:

I am using node to import and export variables and function in my project. In a get-json-data.js file I am importing a modifiyContent function and exporting an userSubscriptions variable:

      var modifiyContent = require('./modifiy-content');

      $.get('https://apiEndpoint.com)
        .done(function(data) {

          for (var key in data.subscriptionsInSet) {
      userSubscriptions.push(data.subscriptionsInSet[key].productName);
          }

          exports.userSubscriptions = userSubscriptions;

          modifiyContent();

      });

And then in the modifiy-content.js file I am importing the file get-json-data.js and trying to use the variable userSubscriptions from it:

var getJsonData = require('./get-json-data');
var userSubscriptions = getJsonData.userSubscriptions;

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

But, I get undefined in the console. What am I doing wrong?



via Leff

No comments:

Post a Comment