简体   繁体   English

NodeJS异步瀑布中的请求返回未定义

[英]Request in NodeJS async waterfall return undefined

I'm pretty new to async on node js.我对节点 js 上的异步很陌生。 I use the waterfall method while parsing a xml file like this:我在解析这样的 xml 文件时使用瀑布方法:

$('situation').each( function(){
var situation = [];
$(this).find('situationRecord').each( function(i){
  var record = this;
  async.waterfall([
    function (callback){
      var filter = {
        startLocationCode: $(record).find('alertCMethod2SecondaryPointLocation').find('specificLocation').text(), 
        endLocationCode: $(record).find('alertCMethod2PrimaryPointLocation').find('specificLocation').text(),
        overallStartTime: $(record).find('overallStartTime').text(),
        overallEndTime: $(record).find('overallEndTime').text()
      }
      callback(null, filter, record);
    },
    function (filter, record, callback){
      var timestamp = new Date().toDateInputValue();
      var startDbResponse = 0;
      var endDbResponse = 0;
      if((filter.startLocationCode != '') && new Date(timestamp) >= new Date(filter.overallStartTime) && new Date(timestamp) <= new Date(filter.overallEndTime) ){
        startDbResponse = locationCodeToGeodataRequst.geodataByLocationcode(filter.startLocationCode);
        endDbResponse = locationCodeToGeodataRequst.geodataByLocationcode(filter.endLocationCode);
      }
      console.log("startDbResponse: ", startDbResponse);
      console.log("endDbResponse: ", endDbResponse);
      callback(null, filter, record, startDbResponse, endDbResponse);
    },
    function (filter, record, startDbResponse, endDbResponse, callback){
     console.log("startDbResponse: ", startDbResponse);
     console.log("endDbResponse: ", endDbResponse);          
     var situationRecord = createSituationRecord($, record, filter.startLocationCode, filter.endLocationCode, startDbResponse, endDbResponse);
      console.log(situationRecord);
    },
    function (situationRecord, callback){
      situation[i] = { situationRecord };
    }
  ],
  function(err, results){
    console.error("There was an error by filtering the xml file");
    console.error(err);
  });
})
if(situation.length > 0){ //if situation is not empty
  locations.push(situation);
}
})
 console.log(locations);
}

In this part of the waterfall I make a request to my database with locationCodeToGeodataRequst.geodataByLocationcode(filter.startLocationCode);在瀑布的这一部分中,我使用locationCodeToGeodataRequst.geodataByLocationcode(filter.startLocationCode);向我的数据库发出请求locationCodeToGeodataRequst.geodataByLocationcode(filter.startLocationCode); but startDbResponse and endDbResponse is undefined :startDbResponseendDbResponseundefined

 ....
 function (filter, record, callback){
      var timestamp = new Date().toDateInputValue();
      var startDbResponse = 0;
      var endDbResponse = 0;
      if((filter.startLocationCode != '') && new Date(timestamp) >= new Date(filter.overallStartTime) && new Date(timestamp) <= new Date(filter.overallEndTime) ){

        startDbResponse = locationCodeToGeodataRequst.geodataByLocationcode(filter.startLocationCode);
        endDbResponse = locationCodeToGeodataRequst.geodataByLocationcode(filter.endLocationCode);

      }
      console.log("startDbResponse: ", startDbResponse);
      console.log("endDbResponse: ", endDbResponse);
      callback(null, filter, record, startDbResponse, endDbResponse);
    },
....

The request by it self works because a console.log in the request module show the correct data.它自己的请求有效,因为请求模块中的 console.log 显示了正确的数据。 So I don't understand why its undefined.所以我不明白为什么它未定义。

This is the request module:这是请求模块:

exports.geodataByLocationcode = function geodataByLocationcode(locationcode){
 let sql = "SELECT * FROM tmc WHERE LOCATION_CODE = " + locationcode;
 let query = db.query(sql, (err, result) =>{
  if(err == null){
    //console.log(result);
    return result;
  }else{
    console.log("Error by getting item from db: " + err);
    throw err;
  }
 });
}

This is a snipped of the console.log:这是 console.log 的一个片段:

....
startDbResponse:  undefined
endDbResponse:  undefined
startDbResponse:  undefined
endDbResponse:  undefined
startDbResponse:  0
endDbResponse:  0
startDbResponse:  0
endDbResponse:  0
[]
//here comes the output of the requests as json objects
.... 

move the console log to the last function of async.将控制台日志移动到 async 的最后一个函数。 As Manjeet pointed out async takes time and console prints early.正如 Manjeet 指出的,异步需要时间并且控制台会提前打印。

  function (situationRecord, callback){
  situation[i] = { situationRecord };
  if(situation.length > 0){ //if situation is not empty
    locations.push(situation);
  }
  })
   console.log(locations);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM