[英]Nodejs: [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, etc. Received an instance of Array
i am trying to run this code but it gives me [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView.我正在尝试运行此代码,但它给了我 [ERR_INVALID_ARG_TYPE]:“数据”参数必须是字符串类型或 Buffer、TypedArray 或 DataView 的实例。 Received an instance of Array.
收到 Array 的实例。
it runs fine on node.js 12.3.1 but gives this error when trying to run on node.js above v14它在 node.js 12.3.1 上运行良好,但在尝试在高于 v14 的 node.js 上运行时出现此错误
i don't know JavaScript Please help我不知道 JavaScript 请帮忙
this is the error logs ↓↓↓这是错误日志↓↓↓
Online at 07-09-2022 7:32PM
'--------------------------------------
node:internal/fs/utils:890
" throw new ERR_INVALID_ARG_TYPE(
^
�TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of Array
) at Object.writeFile (node:fs:2168:5)
6 at Timeout._onTimeout (/home/node/index.js:92:16)
3 at listOnTimeout (node:internal/timers:564:17)
< at process.processTimers (node:internal/timers:507:7) {
code: 'ERR_INVALID_ARG_TYPE'
}
Node.js v18.7.0
This is the code where from i getting this error ↓↓↓↓↓↓这是我收到此错误的代码↓↓↓↓↓↓
// When discord restarts, read from the files
let timeListBackup = fs.readFileSync('timeListBackup.txt').toString().split(",");
for(i in timeListBackup) {
timeList[i] = timeListBackup[i];
}
let firstOfflineBackup = fs.readFileSync('firstOfflineBackup.txt').toString().split(",");
for(i in firstOfflineBackup) {
firstOffline[i] = firstOfflineBackup[i];
}
// Backup the values to a text file
fs.writeFile("timeListBackup.txt", timeList, (err) => {});
fs.writeFile("firstOfflineBackup.txt", firstOffline, (err) => {});
}, 1000);
});
});
As the error says, you are passing arrays ( timeList
and firstOffline
) into writeFile
instead of strings, buffers or the like.正如错误所说,您将 arrays (
timeList
和firstOffline
)传递给writeFile
而不是字符串、缓冲区等。 Perhaps you forgot to JSON.stringify
or join
them.也许您忘记了
JSON.stringify
或join
他们。
In fact, looking at how you read the file (with .split(',')
), it seems you want to use join(',')
on them:事实上,看看你是如何读取文件的(使用
.split(',')
),你似乎想对它们使用join(',')
:
fs.writeFile("timeListBackup.txt", timeList.join(','), (err) => {});
fs.writeFile("firstOfflineBackup.txt", firstOffline.join(','), (err) => {});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.