2017-03-23 7 views
-1

변수 출력을 다른 노드 js 파일로 내보내려고합니다. 하지만 fs 읽기 기능의 비동기 작업 때문에 출력 변수를 내보낼 수 없습니다.비동기 노드 js 파일의 변수를 다른 nodejs 파일로 내보낼 수 없습니다.

내가 실수를하고있는 곳을 이해할 수 없습니다. 출력이 정의되지 않은 것으로 나타납니다. 누구든지 실수를 알 수 있습니까? 이 모듈 B에서 함수를 호출 할 것이다 모듈 A에서

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; 
    } 
    ); 
+0

내 문제를 해결. –

답변

0

콜백 함수를 취 그 (의이 getFileContent를 호출하자) -이 같은 아마 뭔가 : 모듈 A에서 지금

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

를 호출 이 -이 같은 :

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

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

getFileContent가 함수가 아닙니다. –

+0

같은 파일에서 사용 중이지만 다른 파일로 전달할 수없는 경우 출력이 표시됩니다. –

+0

호출이 비동기 적이기 때문에 콜백을 통해 정보를 전달해야합니다. 내보내기 만하면 아직 데이터를 사용할 수 없으므로 정의되지 않습니다. 따라서'getFileContent()'는 콜백을 매개 변수로 취할 함수이며 데이터가 사용 가능 해지면 (얼마 후) 콜백을 통과 할 수 있습니다. – rasmeister

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