当我 运行 代码时出现 'cb' 参数错误
i am getting 'cb' argument error when i run the code
我写了这段代码:
var fs = require('fs');
var data = {
name:'Bob'
}
我得到这个错误:
fs.writeFile('data.json',data) "the 'cb' argument must be of type function. Received undefined"
我该如何解决?
您使用的 the asynchronous callback-style fs.writeFile
API 没有回调(简称 cb
)。
此外,在尝试写入数据之前,您没有将数据编码为 JSON,这样也会失败。
或者:
- 使用
fs.writeFileSync()
进行同步文件写入:
fs.writeFileSync("data.json", JSON.stringify(data));
- 传入回调(可以是任何内容,但必须存在):
fs.writeFile("data.json", JSON.stringify(data), (err) => err && console.error(err));
- use
fs/promises
's promise-based asynchronous API:
var fsp = require('fs/promises');
await fsp.writeFile("data.json", JSON.stringify(data));
我写了这段代码:
var fs = require('fs');
var data = {
name:'Bob'
}
我得到这个错误:
fs.writeFile('data.json',data) "the 'cb' argument must be of type function. Received undefined"
我该如何解决?
您使用的 the asynchronous callback-style fs.writeFile
API 没有回调(简称 cb
)。
此外,在尝试写入数据之前,您没有将数据编码为 JSON,这样也会失败。
或者:
- 使用
fs.writeFileSync()
进行同步文件写入:fs.writeFileSync("data.json", JSON.stringify(data));
- 传入回调(可以是任何内容,但必须存在):
fs.writeFile("data.json", JSON.stringify(data), (err) => err && console.error(err));
- use
fs/promises
's promise-based asynchronous API:var fsp = require('fs/promises'); await fsp.writeFile("data.json", JSON.stringify(data));