Recalling data from a text file javascript

Viewed 73

I'm building a bot to run a game from discord. Players log in and their data is pushed into an array which looks like this:

name: Emily, tag: something#4532, kills: 0, alive: true

Whenever the bot restarts however (which happens a few times a day), all that data is lost.
I know the best way to fix this is to write that information to a file and then recall that file whenever the bot restarts.
I know how to write to a file, but im not sure how to recall that information in way that I could push it back into the array.

1 Answers

If I am following your question correctly, it looks like you just need to read the contents of a file and store it in a variable to use it again. This can easily be done with JSON.parse(). Here is an example I found from the post that was linked in a comment to your post:

var fs = require('fs');
var obj = JSON.parse(fs.readFileSync('file_path_here', 'utf8'));

This should work, assuming you entered the data into the file with JSON.stringify()

Related