`Duplex.from()` 抛出 `"iterable" 参数必须是 Iterable 的一个实例。接收到 Object 的实例
`Duplex.from()` throws `The "iterable" argument must be an instance of Iterable. Received an instance of Object`
我很困惑。 Duplex.from()
方法可以将对象作为 src
并从对象属性 readable
& writable
创建双工流:
converts readable
and writable
into Stream and then combines them into Duplex where the Duplex will write to the writable and read from the readable.
为什么我的示例不起作用?
const { PassThrough, Duplex } = require("stream");
const loopback = new PassThrough();
const duplex = Duplex.from({
writable: loopback,
readable: loopback
});
duplex.on("data", (chunk) => {
console.log("Data on duplex", chunk)
});
duplex.write("Hello World");
我的预期结果是创建双工流,如果是这样,输出与输入相同。 (与“回声”或普通直通流相同)
错误信息:
[nodemon] starting `node index.js`
internal/streams/from.js:32
throw new ERR_INVALID_ARG_TYPE('iterable', ['Iterable'], iterable);
^
TypeError [ERR_INVALID_ARG_TYPE]: The "iterable" argument must be an instance of Iterable. Received an instance of Object
at new NodeError (internal/errors.js:322:7)
at from (internal/streams/from.js:32:11)
at Function.Readable.from (internal/streams/readable.js:1368:10)
at Object.<anonymous> (/home/marc/projects/test-stream/index.js:7:23)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)
at internal/main/run_main_module.js:17:47 {
code: 'ERR_INVALID_ARG_TYPE'
}
有人可以向我解释为什么我不能从 a 对象创建双工流吗?
我有点迷茫,因为文档说您可以将对象作为参数传递,但错误消息说“必须是 Iterable 的实例”。据我所知,一个对象是可迭代的。那么这里的问题是什么?
没关系,我发现了我的错误。
我使用的是过时的节点版本:
node -v
v14.19.0
我通过 nodesource (https://github.com/nodesource/distributions#deb) 更新到 node.js v16:
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt-get install -y nodejs
它按预期工作。
我很困惑。 Duplex.from()
方法可以将对象作为 src
并从对象属性 readable
& writable
创建双工流:
converts
readable
andwritable
into Stream and then combines them into Duplex where the Duplex will write to the writable and read from the readable.
为什么我的示例不起作用?
const { PassThrough, Duplex } = require("stream");
const loopback = new PassThrough();
const duplex = Duplex.from({
writable: loopback,
readable: loopback
});
duplex.on("data", (chunk) => {
console.log("Data on duplex", chunk)
});
duplex.write("Hello World");
我的预期结果是创建双工流,如果是这样,输出与输入相同。 (与“回声”或普通直通流相同)
错误信息:
[nodemon] starting `node index.js`
internal/streams/from.js:32
throw new ERR_INVALID_ARG_TYPE('iterable', ['Iterable'], iterable);
^
TypeError [ERR_INVALID_ARG_TYPE]: The "iterable" argument must be an instance of Iterable. Received an instance of Object
at new NodeError (internal/errors.js:322:7)
at from (internal/streams/from.js:32:11)
at Function.Readable.from (internal/streams/readable.js:1368:10)
at Object.<anonymous> (/home/marc/projects/test-stream/index.js:7:23)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)
at internal/main/run_main_module.js:17:47 {
code: 'ERR_INVALID_ARG_TYPE'
}
有人可以向我解释为什么我不能从 a 对象创建双工流吗?
我有点迷茫,因为文档说您可以将对象作为参数传递,但错误消息说“必须是 Iterable 的实例”。据我所知,一个对象是可迭代的。那么这里的问题是什么?
没关系,我发现了我的错误。
我使用的是过时的节点版本:
node -v
v14.19.0
我通过 nodesource (https://github.com/nodesource/distributions#deb) 更新到 node.js v16:
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt-get install -y nodejs
它按预期工作。