简体   繁体   English

`Duplex.from()` 抛出`“可迭代”参数必须是可迭代的实例。 接收到 Object 的实例

[英]`Duplex.from()` throws `The "iterable" argument must be an instance of Iterable. Received an instance of Object`

Im highly confused.我很困惑。 The Duplex.from() method can take a object as src and creates fromt he object properties readable & writable a duplex stream: Duplex.from()方法可以将 object 作为src并从 object 属性创建一个writable readable双工 stream:

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.readablewritable转换为 Stream,然后将它们组合成 Duplex,其中 Duplex 将写入可写并从可读读取。

Why does my example not work?为什么我的示例不起作用?

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");

My expected outcome is that the a duplex stream is created, and if so, that outputs the same i give as input.我的预期结果是创建了双工 stream,如果是这样,输出与输入相同。 (Same as "echo", or plain passthrough stream) (与“回声”或普通直通流相同)

Error message:错误信息:

[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'
}

Can some one explain to my why i cant create a duplex stream from the a object?有人可以向我解释为什么我不能从 object 创建双工 stream 吗?

Im a bit lost, since the docs say you can pass a object as argument, but the error message says "must be an instance of Iterable".我有点迷茫,因为文档说您可以将 object 作为参数传递,但错误消息说“必须是 Iterable 的实例”。 As far as i know, a object is iterable.据我所知, object 是可迭代的。 So whats the problem here?那么这里的问题是什么?

Never mind, i found my mistake.没关系,我发现了我的错误。

I was using a outdated node verison:我使用的是过时的节点版本:

node -v
v14.19.0

I updated to node.js v16 via nodesource ( https://github.com/nodesource/distributions#deb ):我通过 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

And it worked as expected.它按预期工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “订阅字段必须返回异步可迭代。 收到:未定义” - apollo “Subscription field must return Async Iterable. Received: undefined” “原始”参数必须是 function 类型。接收到 Object 的实例 - The "original" argument must be of type function. Received an instance of Object 反应回调返回不可迭代的实例 - React callback return not iterable instance TypeError:无效的解构不可迭代实例的尝试。 为了可迭代,非数组对象必须有一个 [Symbol.iterator]() - TypeError: Invalid attempt to destructure non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() 对不可迭代实例的解构尝试无效。 为了可迭代,非数组对象必须有一个 [Symbol.iterator]() 方法 - Invalid attempt to destructure non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() method ERROR 错误:未捕获(承诺):TypeError:this.products 不可迭代。 (但它是可迭代的) - ERROR Error: Uncaught (in promise): TypeError: this.products is not iterable. (but it is iterable) TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。 收到 Object 的实例 - TypeError [ERR_INVALID_ARG_TYPE]: The “path” argument must be of type string. Received an instance of Object 使普通对象可迭代。(javascript) - Making plain objects iterable.(javascript) ERR_INVALID_ARG_TYPE - “from” 参数必须是字符串类型。 接收到一个 Array 实例 - ERR_INVALID_ARG_TYPE - The “from” argument must be of type string. Received an instance of Array \"path\" 参数必须是字符串类型或 Buffer 或 URL 的实例。 从 nodejs 收到 undefined - The \"path\" argument must be of type string or an instance of Buffer or URL. Received undefined from nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM