2
I am writing a script using Node.JS that checks how many points a customer has in their registration, and I need that every purchase above 150 real that he makes, the point increase by 1, when you reach 10, the customer has an automatic discount of 25%
I need to overwrite information in a . json file, but when I call Fs.writeFile() it overwrites the entire file
my code:
const data = fs.readFileSync('test.json', 'utf8')
var document = JSON.parse(data)
command(client, 'test_pay', (message) => {
var cont = message.content.replace('%test_pay', '').split(' ')
var value = cont[0]
var clientId = cont[1] - 1
var clientScore = 9 //document.clients[clientId].score
var client = document.clients[clientId].nome
var valueOff = value - ((value * 25) / 100)
if (value >= 150) {
clientScore += 1;
var data = JSON.stringify(clientScore, null, 2)
fs.writeFile('test.json', data, finished)
function finished(err) {
console.log('all set.')
}
console.log("=====================")
console.log(`Mensagem: ${message.content}`)
console.log(`Valor: ${value}`)
console.log(`Id do Cliente: ${clientId}`)
console.log("=====================")
console.log(`Cliente: ${client}`)
console.log(`Valor pago: R$${value}`)
console.log(`Score: ${clientScore}`)
console.log("=====================")
console.log()
} else {
console.log("=====================")
console.log(`Mensagem: ${message.content}`)
console.log(`Valor: ${value}`)
console.log(`Id do Cliente: ${clientId}`)
console.log("=====================")
console.log(`Cliente: ${client}`)
console.log(`Valor pago: R$${value}`)
console.log(`Score: ${clientScore}`)
console.log("=====================")
console.log()
}
if (clientScore === 10 && value >= 150) {
clientScore = 0;
console.log("=====================")
console.log(`10 scores! Desconto de 25%`)
console.log(`Scores: ${clientScore}`)
console.log(`De: R$${value} por R$${valueOff}`)
console.log("=====================")
console.log()
}
})
this is my . json file before this part of the code is executed:
need to overwrite "score": 0 for "score": 1
and that’s how it looks after that part of the code is executed:
ps.: at the beginning of the code has command(client, 'test_pay', (message) => { °codigo° }, in addition to Node.JS, I am using Discord.JS, some things of this code are from Discord.JS itself, such as the message.content, that takes the contents of the string, and makes a replace of:
for
With all due respect, but append "gross" in JSON does not seem very valid, since from the first append the syntactic structure of JSON will have already been invalidated.
– Luiz Felipe
It’s just an example, but because it’s not very valid?
– Dakota
Imagine that you have a JSON in which the first level is an object (but it would also become invalid in case of array). Something like:
{ ... }
. So far, is valid. But imagine that an append is made in the archive, then we have something like:{ ... } { ... }
. Do you see the problem? From the first append one invalidates the syntactic structure of JSON, which would prevent parse for the next use.– Luiz Felipe
The answer is good, only this detail that was problematic. One thing you can do is: when reading the file, parse is done (
JSON.parse
). You make the modifications to the object and then you transform it into a JSON string (JSON.stringify
). Then you can over-write all the file contents by the string (in JSON format) referring to the new data. There is no problem of syntactic invalidation. If you want to edit the answer to fix that problem, I will be happy to give the +1. :-)– Luiz Felipe
Yes, I understood what you meant, I understood the problem of the solution I pointed out, but I did not understand the solution you proposed very well, I tried to apply +- so I understood that it did not work very well could explain me better kkk, sorry.
– Dakota
You can do something more or less thus.
– Luiz Felipe
Yes, I’m going to do what you said but I posted a question here in the stack overflow about this, I want to know if there’s a way to do this that you said without changing much in the code, because it has some solutions but it changes a lot, I want to know if it is possible to change the least possible pq also use the append file in some situations and I would like to know if it is possible to do this.
– Dakota
Edited hehe :)
– Dakota