【问题标题】:How to use function callbacks in Nodejs with xml2js如何在带有 xml2js 的 Nodejs 中使用函数回调
【发布时间】:2015-01-28 15:38:04
【问题描述】:

我正在使用 xml2js 来解析 Nodejs 中的 XML 文档。

XML 文件是远程托管的,所以我使用 Node.js http 请求来获取 xml 数据,然后使用 xml2js 解析它,如下所示:

var parser = new xml2js.Parser();

function getValue(){
    var options = {
      hostname: myHost,
      path: myPath,
      method: 'GET',
      headers: {
          'Authorization': myAuthString
      }
    };

    var req = http.request(options, function(res) {
      res.on('data', function (response) {
        parser.parseString(response); 
      });
    });
    req.end();
}
parser.on('end', function(result) {
    // Output the distribution plans to console
     eyes.inspect(result);
     // Get the value that I want to use
     var returnThis = result['myKey'];
});

上面的代码有效,当http请求接收到“数据”事件时,我得到XML数据,然后xml2js解析器解析XML,我在解析的“结束”事件上得到数据。

我的问题是,如何使用回调将“returnThis”变量值返回给 getValue 函数?

例如,如果我从 XML 中检索人名,我希望这样可以工作:

console.log("The returned name is: " + getValue());

任何建议将不胜感激!蒂亚!

【问题讨论】:

  • 可以随时给解析发送一个回调,在它完成后调用,或者你可以返回promise,或者抛出事件。一个不太吸引人的选择是将其设置为具有两种功能的范围
  • @AbrahamAdam 感谢您的建议,我对回调不熟悉,您能否发布一个发送解析回调的示例?非常感谢!
  • 删除了我的答案,因为@yzarubin 的答案很好地展示了回调的想法,我会为事件的想法写另一个。

标签: javascript node.js callback


【解决方案1】:

尝试以下方法:

function getValue(callback){
    var options = {
      hostname: myHost,
      path: myPath,
      method: 'GET',
      headers: {
          'Authorization': myAuthString
      }
    };

    var req = http.request(options, function(res) {
      var parser = new xml2js.Parser();
      parser.on('end', function(result) {
        // Output the distribution plans to console
        eyes.inspect(result);
        callback(null, result['myKey'])
      });
      res.on('data', function (response) {
        parser.parseString(response); 
      });
    });
    req.end();
}

要使用你的函数,你需要这样调用你的函数:

getValue(function(err, res){
  // res should equal myKey
  console.log(res)

})

【讨论】:

    【解决方案2】:

    事件示例

    var emitter = require('events').EventEmitter,
        parseEmitter = new emitter();
    function getValue(trigger){
        var options = {
          hostname: myHost,
          path: myPath,
          method: 'GET',
          headers: {
              'Authorization': myAuthString
          }
        };
    
        var req = http.request(options, function(res) {
          var parser = new xml2js.Parser();
          parser.on('end', function(result) {
            // Output the distribution plans to console
            eyes.inspect(result);
            callback(null, result['myKey'])
          });
          res.on('data', function (response) {
            parser.parseString(response); 
          });
        });
        parser.on('end', function(result) {
          // Output the distribution plans to console
          eyes.inspect(result);
          // Get the value that I want to use
          var returnThis = result['myKey'];
          trigger.emit('log', returnThis);
        });
        req.end();
    }
    parseEmitter.on('log', function(value) {
      console.log('The return name is '+value);
    }
    getValue(parseEmitter);
    

    【讨论】:

      猜你喜欢
      • 2020-07-31
      • 2015-06-09
      • 2014-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-25
      相关资源
      最近更新 更多