-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
  • 756,363
  • 169
  • 1,062
  • 1,111
Godz
  • 1
  • 3

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
  • 3
savageGoat
  • 1,118
  • 1
  • 2
  • 7