0

As seen here: https://anidiots.guide/coding-guides/storing-data-in-a-json-file.html

It shows you how to create a point system in discord.js. But what caught my eye is how they used let points = JSON.parse(fs.readFileSync("./points.json", "utf8")); to read the file. So i am trying to learn how to make a database where i get the points plus money that can be redeemed daily and shared. kinda like a bank. but i don't know how to do that. If anyone could help me with a hastebin link or anywhere i can learn in depth how to use the JSON.parse(fs.readFileSync("./points.json", "utf8")); thing.

and if you want to see my bot in action don't hesitate to use https://discord.me/knut

iehrlich
  • 3,572
  • 4
  • 34
  • 43
Volley God
  • 11
  • 2

2 Answers2

0

The line you're asking about is made of two call to the functions JSON.parse and fs.readFileSync.

  • JSON.parse. This function receives a bunch of text and transform it (parse it) into a javascript object. It can be very useful when you want to, for example, build something dynamically based on the content of a file. Maybe w3school is a good place to start looking for info about it.

Example

var string = "{id: 4, name:'Volley'}"
var parseObject = JSON.parse(string)
console.log(parseObject.id); //4
console.log(parseObject.name); //Volley
  • fs.readFileSync. As you probably know, most of the functions in javascript and node.js are asynchronous, that is, instead of calling and get the returned value, you have to define a callback within which you would use the value you want. fs.readFileSync is just the synchronous version of fs.readFile(callback), which returns the content of the read file. Here you have the docs about that function.

These functions are actually simple to use, you should struggle in finding some examples or trying them by yourself.

If you want to imitate what the tutorial said, then you would need to define another file, with the money of each point, or edit the first file if you can, so you could an object like

var point_and_money = {
    points : [...],
    money : [....]
 }

or two objects with the separate information

var points = JSON.parse(fs.readFileSync("./points.json", "utf8"));
var money = JSON.parse(fs.readFileSync("./money.json", "utf8"));

Hope I gave you a hint about what you asked

J. Maria
  • 362
  • 3
  • 14
0

not really sure what you are trying to achieve?

JSON.parse(fs.readFileSync("./points.json", "utf8"));

This line reads a json file and parse it to a Javascript-Method. Nothing less and nothing more. this can also be done in Nodejs via

var points = require('./points.json');

You mentioned something like how to do a database? Basically I am not sure if you want to develop a database or better use an existing one. Look for MongoDB, SQLLite,IndexedDB, etc. There a tons of database for almost every use case. Remember that your line of code reads synchronous in a blocking way when the file gets large. And when multiple users would access the file at the same time you need to handle this somehow. So definitely would suggest to look for some existing database solution and have more time to focus on your business logic.

I hope I understand your question correct and my answer helps.

Maybe this one is also a good question to start: Lightweight Javascript DB for use in Node.js

silverfighter
  • 6,762
  • 10
  • 46
  • 73