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. Received an instance of Array.
it runs fine on node.js 12.3.1 but gives this error when trying to run on node.js above v14
i don't know JavaScript Please help
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);
});
});
CodePudding user response:
As the error says, you are passing arrays (timeList
and firstOffline
) into writeFile
instead of strings, buffers or the like. Perhaps you forgot to JSON.stringify
or join
them.
In fact, looking at how you read the file (with .split(',')
), it seems you want to use join(',')
on them:
fs.writeFile("timeListBackup.txt", timeList.join(','), (err) => {});
fs.writeFile("firstOfflineBackup.txt", firstOffline.join(','), (err) => {});