Node.js - “listener”参数必须是Function类型

问题描述 投票:0回答:1

我正在尝试使用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时,它工作正常

node.js macos http
1个回答
3
投票

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

v8.9.3中,它是对ReadStream.prototype.close的引用,其中回调参数是可选的。

v9.3.0,它现在是一个单独的方法,除其他外,发出close事件:

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

你得到的错误是由this.on('close', cb)引起的,它需要Function第二个参数,而你的代码中没有传递这个参数。

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

© www.soinside.com 2019 - 2024. All rights reserved.