0

I'm trying to create some simple statistics in the .JSON file, I would like to count each command that was issued, but I'm unable to save increment value in the .JSON file.

.JSON

{
   "stats": {
      "value": 0,
      "points": 0,
      "commandUsed": 0
   }
}

code:

const fs = require('fs');

let statistics = fs.readFileSync(__dirname + '/stats.json', 'utf8');
let stats = JSON.parse(statistics)
console.log(stats)

//stats
let value = stats['stats']['value']
let points = stats['stats']['points']
let usedCommands = stats['stats']['commandUsed']


usedCommands++ 
console.log(usedCommands) //logs actual amount of issued commands
fs.writeFileSync(__dirname + '/stats.json', JSON.stringify(stats, 0, 4), 'utf8')

The command count is not increasing in the .JSON file.

RasmonT
  • 391
  • 2
  • 13
  • Besides the fact that `commandUsed` looks like a string instead of a number, you never *assign* to `stats` or any of its properties. `stats['stats']['commandUsed'] = usedCommands;`? – crashmstr Mar 28 '22 at 18:00
  • There is only a declaration `let usedCommands = stats['stats']['commandUsed']`, I want to store numbers in these .JSON, in this example exactly the count of the commands via increment, with each update I thinked that it will edit the object value from 1 then 2 3 4 etc... So I need to do something else in order to store increment in the objects? – RasmonT Mar 28 '22 at 18:09
  • I understand about the string, yeah that's my mistake, I edited the strings to numbers. – RasmonT Mar 28 '22 at 18:12
  • `usedCommands` is a number or a string. Those are not reference types. Changes to `usedCommands` does not change the "source" you got it from. Thus, you need to assign back to `stats` before writing `stats` out. – crashmstr Mar 28 '22 at 18:24
  • I think you meant just: `stats['stats']['commandUsed']++` – Wyck Mar 28 '22 at 20:27

1 Answers1

2

There are a couple of things I noticed. You had a few vars that you were not using and what you were trying to "increment" was a string in your JSON file (updated). Try this.

const fs = require("fs");

const statistics = fs.readFileSync(__dirname + "/stats.json", "utf8");
const { stats } = JSON.parse(statistics);

let commandUsed = stats["commandUsed"];
commandUsed++;

const updatedStats = { stats: { ...stats, commandUsed } };

fs.writeFileSync(
  __dirname + "/stats.json",
  JSON.stringify(updatedStats, 0, 4),
  "utf8"
);
{
    "stats": {
        "commandUsed": 0,
        "points": 0,
        "value": 0
    }
}
PCDSandwichMan
  • 1,964
  • 1
  • 12
  • 23
  • It says that commandUsed is undefined for some reason, whenever I change the `const updatedStats = { stats: { ...stats, commandUsed } };` for example to `const updatedStats = { stats: { ...stats, cmd } };` It will create new object and it will count there, it's not actually reading from the commandUsed object . – RasmonT Mar 28 '22 at 18:28
  • Did you copy-paste exactly what was there? I just tested it again and it seems to be working. – PCDSandwichMan Mar 28 '22 at 18:43
  • It works, I was using my old variable so that's why I had an undefined error. Thank you! – RasmonT Mar 28 '22 at 18:58