-2

My current code is below.

bot.on('message', mssg => {
    io.emit('chat message', `${mssg.author.tag} - ${mssg.content}`);
    
    var message = mssg.content.toLowerCase()
    if(shydudearray.includes(message)) {
        var newMsg = message.replace(" ", "");
        fs.readFile('./assets/data/shydude.json', (err, data) => {
            if (err) throw err;
            let counter = JSON.parse(data);
            
            x = counter[newMsg];
            data[newMsg] = '2';
            
            console.log(`Before: ${x}\nAfter: ${counter[newMsg]}`)
        });
    }
});

I want to grab my JSON file and get the current number, then +1 to the current number, and save it to the JSON file. Using FS/Javascript, how could I get this to work?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Godz
  • 1
  • 1
  • 4

1 Answers1

1

Here

const fs = require('fs');
const filepath = './myJSONfile.json';
const data = JSON.parse(fs.readFileSync(filepath));
data[someKey] = "newValue";
fs.writeFileSync(filepath, JSON.stringify(data, null, 4));

PS: This is a NodeJS question more than JavaScript

Edit From Question Asker: Fixed Variable Mistake

Godz
  • 1
  • 1
  • 4
savageGoat
  • 1,389
  • 1
  • 3
  • 10