gpt4 book ai didi

javascript - 使用 .pipe() 时出现 node.js fs.createReadStream 错误

转载 作者:太空宇宙 更新时间:2023-11-04 02:36:16 24 4
gpt4 key购买 nike

使用此函数,我尝试将输入呈现到文件

function Write(err, content, output) {
if (err) { console.error(err) };
var fs = require('fs');
var inp = fs.createReadStream(content);
var out = fs.createWriteStream(output);
inp.pipe(out);
}

例如...

write('some text', 'write-test.txt');

但是运行上述内容作为测试,我得到以下返回

2) The Write module should write to test.txt:
TypeError: path must be a string
at Object.fs.open (fs.js:418:11)
at open (/Users/joshburns/Code/micro-format/node_modules/grunt/node_modules/rimraf/node_modules/graceful-fs/graceful-fs.js:60:16)
at Object.gracefulOpen [as open] (/Users/joshburns/Code/micro-format/node_modules/grunt/node_modules/rimraf/node_modules/graceful-fs/graceful-fs.js:45:3)
at WriteStream.open (fs.js:1654:6)
at new WriteStream (fs.js:1644:10)
at Object.fs.createWriteStream (fs.js:1608:10)
at Write (/Users/joshburns/Code/micro-format/lib/write.js:9:450)
at Context.<anonymous> (/Users/joshburns/Code/micro-format/test/write/index.js:8:4)
at Test.Runnable.run (/Users/joshburns/Code/micro-format/node_modules/grunt-mocha-test/node_modules/mocha/lib/runnable.js:211:32)
at Runner.runTest (/Users/joshburns/Code/micro-format/node_modules/grunt-mocha-test/node_modules/mocha/lib/runner.js:358:10)

最佳答案

示例中的第一个参数是 err 而不是 content,并且 outputundefined,这就是错误消息的原因。

另外:阅读文档:fs.createReadStream需要文件名,而不是内容。您可能想要的是 fs.writeFilefs.openfs.write 结合.

关于javascript - 使用 .pipe() 时出现 node.js fs.createReadStream 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22084336/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com