无法将异步节点js文件中的变量导出到其他nodejs文件

时间:2017-03-23 22:21:38

标签: javascript node.js express node-async

我正在尝试将变量输出导出到另一个节点js文件。但是由于fs读取函数的异步任务,我无法导出输出变量。

我无法理解我犯错误的地方。 我只是输出未定义。 谁能告诉我这个错误。

    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 个答案:

答案 0 :(得分:0)

从模块A开始,你需要调用模块B中的一个函数(我们称之为getFileContent),它接受一个回调函数 - 可能是这样的:

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

现在在模块A中,调用它 - 类似这样:

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

B.getFileContent(function(err, result) {
  if (err) {
    :
  } else {
    // do something with result
  }
});

答案 1 :(得分:0)

您现在正在导出任何内容,因为您正在异步调用函数,因此您应该导出函数而不是空对象。例如:

在您的主文件中

var awesomeExports  = require('seriesFile');

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

async.series档案

//All your includes.

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