-1

I am trying to export variable output to another node js file. But due to async task of fs read function, I am unable to export output variable.

I am unable to understand where I am making mistake. I am just getting output as undefined. Could anyone let me know the mistake.

    var parseString = require('xml2js').parseString;
    var xml = '';
    var fs = require('fs');
    var async = require('async');
    var exports = module.exports = {};
    var output;
    var out;

    async.series([
    function (callback) {
    fs.readFile('./sample.xml', 'utf8', function(err, data) {
        parseString(data, function(err, result) {
            xml = result;
            var partyNames = xml["TXLife"]["TXLifeRequest"][0]["OLifE"][0]["Party"];

            for (var i = 0;i < partyNames.length;i++) {
                var firstName, lastName, sex, dob, zip, riskScore, scriptCheckScore, questCheckScore;
                if (partyNames[i]["PartyTypeCode"][0]["_"] == "Person" && partyNames[i]["Person"][0]["LastName"] == "JAYME") {
                    if (partyNames[i]["Person"][0].hasOwnProperty("FirstName")) {
                        firstName = partyNames[i]["Person"][0]["FirstName"];
                    }

                    if (partyNames[i]["Person"][0].hasOwnProperty("LastName")) {
                        lastName = partyNames[i]["Person"][0]["LastName"];
                    }

                    if (partyNames[i]["Person"][0].hasOwnProperty("BirthDate")) {
                        dob = partyNames[i]["Person"][0]["BirthDate"];
                    }

                    if (partyNames[i]["Person"][0].hasOwnProperty("Gender") && partyNames[i]["Person"][0]["Gender"][0].hasOwnProperty("_")) {
                        sex = partyNames[i]["Person"][0]["Gender"][0]["_"]
                    }

                     if (partyNames[i].hasOwnProperty("Address") && partyNames[i]["Address"][0].hasOwnProperty("Zip")) {
                        zip = partyNames[i]["Address"][0]["Zip"][0];
                     }

                     if (partyNames[i].hasOwnProperty("Risk") && partyNames[i]["Risk"][0].hasOwnProperty("OLifEExtension") && 
                        partyNames[i]["Risk"][0]["OLifEExtension"][5].hasOwnProperty("RiskScoring") && partyNames[i]["Risk"][0]["OLifEExtension"][5]["RiskScoring"][0].hasOwnProperty("RiskScore")) {

                        riskScore = partyNames[i]["Risk"][0]["OLifEExtension"][5]["RiskScoring"][0]["RiskScore"][0]["QuantitativeScore"][0];
                        scriptCheckScore = partyNames[i]["Risk"][0]["OLifEExtension"][5]["RiskScoring"][0]["RiskScore"][1]["QuantitativeScore"][0];
                        questCheckScore = partyNames[i]["Risk"][0]["OLifEExtension"][5]["RiskScoring"][0]["RiskScore"][2]["QuantitativeScore"][0]
                        console.log("Risk score ",riskScore);
                        console.log("Script check score ",scriptCheckScore);
                        console.log("questCheckScore ",questCheckScore);
                     }
                     output = firstName + " " + lastName + " " + dob + " " + sex + " " + zip; 
                     callback(null, output);
                }   
            }

        })
    });
    },

    function (callback){
        out = output;
        //module.exports.out = output;
        console.log("second");
        callback(null, out);
    }
    ],

    function(err, result) {
        console.log("result", result);
        exports.out = result;
    }
    );

2 Answers2

0

From Module A you will want to invoke a function in Module B (let's call it getFileContent) that takes a callback function - maybe something like this:

var getFileContent(callback) {
   :
   // async operation to get content
   callback(null, results); // assuming no error
   :
}

Now in Module A, invoke this - something like this:

var B = require('B');   // whatever module B reference is

B.getFileContent(function(err, result) {
  if (err) {
    :
  } else {
    // do something with result
  }
});
rasmeister
  • 1,986
  • 1
  • 13
  • 19
  • getFileContent is not a function? – Santhosh Mohan Mar 24 '17 at 00:47
  • I am getting the output if I am using in the same file but I am unable to pass to another file. – Santhosh Mohan Mar 24 '17 at 00:59
  • Because the call is asynchronous, you need to pass the information through a callback. If you just export, the data won't yet be available so you get undefined. So `getFileContent()` is a function you would create that will take a callback as a parameter and when the data is available (some time later) it can be passed through that callback. – rasmeister Mar 24 '17 at 02:01
0

You are exporting nothing right now because you're calling a function asynchronously, so you should be exporting your function instead of an empty object. For example:

In your main file

var awesomeExports  = require('seriesFile');

awesomeExports((err, value) => {
    if(err) //Do something with error.
    //Do something with value.
})

In your async.series file

//All your includes.

module.exports = (err, callback) => {
  async.series([
      //Your async.series functions in the array.
    ],
    function(err, result) {
        callback(err, result);
    }
  );
}
forrestmid
  • 1,494
  • 17
  • 25