0

It keeps adding an extra } to the json file and wont add 1 to the level

I have tried to have it saving the original level value to a txt file then waiting a second then adding 1 to it.

    if(xpout > 100) {
        var lvupxp = xp.xp - 100
        var expcontent = `{ "xp": ${lvupxp}, "level": ${xp.level++} }`
        var exppath = `./Users/${message.author.username}/xp.json`;
        fs.writeFile(exppath, expcontent, (err) => {
            if (err) throw err;
        })

    }

{ "xp": 7, "level": 0 } }

JayRizuri
  • 3
  • 2
  • [I can't reproduce that extra brace](https://jsfiddle.net/2cthesg1/). But if you want to increase the level use the prefix `++xp.level` instead. – Andy Dec 22 '18 at 00:09

1 Answers1

2

If you want the level property to contain the incremented level, you have to use pre-increment, not post-increment. See ++ operator returns original value if placed after operand — how?

Also, don't create JSON with string operations. Create an object and use JSON.stringify().

var expcontent = JSON.stringify({cp: lvupxp, level: ++xp.level});
Barmar
  • 741,623
  • 53
  • 500
  • 612