Hello, im using js server side, and time to time my json files are being cleared. They get empty, than i get error “SyntaxError: /app/welcomer.json: Unexpected end of JSON input” becouse it has no {}… Idk why is that happening.
Im doing discord bot, im loading with
const servers = require("./servers.json")
in front of message event
and saving with
fs.writeFile("./servers.json", JSON.stringify(servers), (err) =>{ if(err) console.log(err); });
in message event
and there are lines of code doing with Json in message event.
Could be the error that the JSON file was not saved after loading?
To read JSON files you don’t use require, use fs.
Example:
const servers = JSON.parse(fs.readFileSync("./servers.json"))
2 Likes
Do you think its the problem and it wont get deleted anymore? i have 5 json files and all got deleted at same time
Yes, because it’s probably stringifying null and saving it.
2 Likes
Ty, ill write if the issue will repeat
Id also like to point out that JSON files can easily corrupt.
Meaning that you should use some kind of data base.
Hey @TGamingStudio!
Adding onto what @Apollyon365 said, you should really on JSON to store data since the Node.js file system module (fs) corrupts the JSON file while reading and writing the data at the same time.
I would recommend Endb or a database driver instead of relying on fs.
Happy Glitching!
1 Like