问题描述
我正在尝试使用node.js创建代理更新代码,并且我收到此错误:
I am trying to create a proxy update code with node.js, and im getting this error:
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时,它工作正常
Im on MacOS Sierra with node.js v9.3.0.
apparently, when I use node.js v8.9.3, it works fine
推荐答案
在v8.9.3和v9.3.0之间,执行 WriteStream.prototype.close
已更改。
Between v8.9.3 and v9.3.0, the implementation of WriteStream.prototype.close
has changed.
在,它是对,for哪个回调参数是可选的。
In v8.9.3, it was a reference to ReadStream.prototype.close
, for which a callback argument was optional.
在,它现在是一个单独的方法,除其他外,发出关闭
事件:
In v9.3.0, it is now a separate method that, amongst other things, emits a close
event:
WriteStream.prototype.close = function(cb) {
if (this._writableState.ending) {
this.on('close', cb);
return;
}
...
};
您收到的错误是由 this.on('close'引起的,cb)
,需要 函数
第二个参数未在代码中传递。
The error that you get is caused by this.on('close', cb)
, which requires a Function
second argument that isn't being passed in your code.
我不确定你是否真的需要在你的情况下使用完成
处理程序,因为可写处理将是由 .pipe()
代码在内部完成。
I'm not sure if you actually need to use a finish
handler at all in your situation, as writable handling will be done internally by the .pipe()
code.
这篇关于Node.js - “监听器”参数必须是Function类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!