Node.js - "听众"参数必须是Function类型

时间:2017-12-24 16:56:15

标签: node.js macos http

我正在尝试使用node.js创建代理更新代码,并且我收到此错误:

    events.js:180
    throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'listener', 'Function');
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type Function
    at _addListener (events.js:180:11)
    at WriteStream.addListener (events.js:240:10)
    at WriteStream.close (fs.js:2298:10)
    at WriteStream.<anonymous> (/Users/camden/Desktop/proxyupdate/u.js:9:15)
    at WriteStream.emit (events.js:164:20)
    at finishMaybe (_stream_writable.js:616:14)
    at afterWrite (_stream_writable.js:467:3)
    at onwrite (_stream_writable.js:457:7)
    at fs.write (fs.js:2242:5)
    at FSReqWrap.wrapper [as oncomplete] (fs.js:703:5)

这是我的代码:

var UpdateProxyList = function(sourceURL, destinationPath) {
  var HTTP = require("http");
  var FS = require("fs");
  var File = FS.createWriteStream(destinationPath);
  HTTP.get(sourceURL, function(response) {
    response.pipe(File);
    File.on('finish', function() {
      File.close();
    });
    File.on('error', function(error) {
      FS.unlink(destinationPath);
    })
  });
}
UpdateProxyList("http://www.example.com/proxy.txt", "myproxylist.txt");

我在MacOS Sierra上使用node.js v9.3.0 显然,当我使用node.js v8.9.3时,它工作正常

1 个答案:

答案 0 :(得分:3)

在v8.9.3和v9.3.0之间,WriteStream.prototype.close的实现已经改变。

actors中,它是v8.9.3的引用,回调参数是可选的。

ReadStream.prototype.close中,它现在是一个单独的方法,除其他外,会发出close事件:

WriteStream.prototype.close = function(cb) {
  if (this._writableState.ending) {
    this.on('close', cb);
    return;
  }
  ...
};

您收到的错误是由this.on('close', cb)引起的,要求 Function第二个参数,但不会在您的代码中传递。

我不确定您是否真的需要在您的情况下使用finish处理程序,因为可写处理将由.pipe()代码在内部完成。