这个 Node JS 代码来自我试图理解的 这个项目。
// initialize the next block to be 1
let nextBlock = 1
// check to see if there is a next block already defined
if (fs.existsSync(configPath)) {
// read file containing the next block to read
nextBlock = fs.readFileSync(configPath, "utf8")
} else {
// store the next block as 0
fs.writeFileSync(configPath, parseInt(nextBlock, 10))
}
我收到此错误消息:
Failed to evaluate transaction: TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received type number (1)
我对 NodeJS 不太熟悉,所以有人可以向我解释如何更改此代码以消除错误吗?
原文由 user10931326 发布,翻译遵循 CC BY-SA 4.0 许可协议
所以错误是说
data
( fs.writeFileSync 函数的第二个参数)应该是一个字符串或一个缓冲区……等等,而是得到一个数字。要解决此问题,请将第二个参数转换为字符串,如下所示: