I don't know what I'm doing wrong. I've done everything 'perfect' from my knowledge.
The error is happening at writeParams. Why is this happening?
Code
const fs = require('fs');
const path = require('path');
const paramsPath = path.join(__dirname, 'params.json')
function writeParams(data) {
console.log("Writing params.json file. . .", data);
return fs.writeFileSync(paramsPath, JSON.stringify(data))
}
function readParams() {
console.log("Reading 'params.json' file. . .");
const data = fs.readFileSync(paramsPath);
return JSON.parse(data.toString());
}
Error
TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined at Object.writeFileSync (node:fs:2146:5) at writeParams (E:\TB\JS\index.js:8:15) at Timeout.main [as _onTimeout] (E:\TB\JS\index.js:85:9) at processTicksAndRejections (node:internal/process/task_queues:96:5)
I recreated your error. Your code is completely valid and works as long as you pass writeParams a valid Javascript object. The only way your error could occur would be if you forgot to give writeParams some data.
Mistakes like this happen often and can be extremely frustrating. If you are getting too frustrated at a program, your ability to fix the problem can be hindered. I'd recommend doing some other tasks (taking a walk, doing the mountain of dishes in your sink, etc.) and coming back to programming next time an error like this occurs.
Heres my test code:
const fs = require('fs');
const path = require('path');
const paramsPath = path.join(__dirname, 'params.json')
function writeParams(data) {
console.log("Writing params.json file. . .", data);
return fs.writeFileSync(paramsPath, JSON.stringify(data))
}
function readParams() {
console.log("Reading 'params.json' file. . .");
const data = fs.readFileSync(paramsPath);
return JSON.parse(data.toString());
}
writeParams({foo: "bar"})
console.dir(readParams())