0

我想通过在函数中解析一个warc文件来将JSON数据分配给一个变量。该变量在函数外部不可访问,并在控制台上返回一个空数组。

var metadataObj = {
  metadata: []
};
fs
  .createReadStream('mywarc-file.warc')
  .pipe(new WARCStreamTransform())
  .on('data', record => {
    if (targetURL === record.warcHeader['WARC-Target-URI']){
      if(record.warcHeader['WARC-Type'] === 'response'){
        metadataObj.metadata.push({
          Url: record.warcHeader['WARC-Target-URI'],
          WarcID:record.warcHeader['WARC-Warcinfo-ID'],
          Timestamp:record.warcHeader['WARC-Date'],
          ContentType:record.warcHeader['Content-Type']
        })
      }else{
        metadataObj.metadata.push({
          Host: record.httpInfo.headers['Host'],
          userAgent: record.httpInfo.headers['User-Agent']
        })
      }
    }
  })
console.log(metadataObj.metadata)
4

1 回答 1

0

实际上,变量 metadataObj 是可访问的,并且可能已设置。问题是,在设置新变量值之前正在执行 console.log 语句。

我建议您阅读有关 javascript 中的异步代码处理和变量范围的更多信息。

这是您修复的代码:

var metadataObj = {
  metadata: []
};
fs
  .createReadStream('mywarc-file.warc')
  .pipe(new WARCStreamTransform())
  .on('data', record => {
    if (targetURL === record.warcHeader['WARC-Target-URI']){
      if(record.warcHeader['WARC-Type'] === 'response'){
        metadataObj.metadata.push({
          Url: record.warcHeader['WARC-Target-URI'],
          WarcID:record.warcHeader['WARC-Warcinfo-ID'],
          Timestamp:record.warcHeader['WARC-Date'],
          ContentType:record.warcHeader['Content-Type']
        })
      }else{
        metadataObj.metadata.push({
          Host: record.httpInfo.headers['Host'],
          userAgent: record.httpInfo.headers['User-Agent']
        })
      }
    }
  })
  .on('end', () => {
    console.log(metadataObj.metadata)
  })

有用的阅读来源:

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Introducing#Asynchronous_JavaScript

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

https://medium.com/codebuddies/getting-to-know-asynchronous-javascript-callbacks-promises-and-async-await-17e0673281ee

https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-promise-27fc71e77261

于 2019-01-09T07:33:38.397 回答