Fs.writeFile() is overwriting a file

Asked

Viewed 215 times

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:

inserir a descrição da imagem aqui need to overwrite "score": 0 for "score": 1

and that’s how it looks after that part of the code is executed:

inserir a descrição da imagem aqui

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:

inserir a descrição da imagem aqui

for

inserir a descrição da imagem aqui

2 answers

1

The method fs.writefile() replaces the file every time the method is called, if it does not exist will be created the file with the content you put, if it exists it will overwrite, therefore you should use the method fs.appendFile() If your file does not exist it will create and add the content, if it exists it will add without replacing anything. This is one more example is just adapt to your:

const fs = require('fs');                                     
constt file = __dirname + '/teste.json';

const produto = {
 nome: 'Smartphone',
 preco: 1749.99,
 desconto: 0.15
};                                                              
fs.appendFile('mynewfile2.txt', JSON.stringify(produto), err => {
 console.log(err || 'Arquivo salvo');
});

Now in a way that will not invalidate the syntax of json

const { readFile, writeFile } = require('fs').promises;                                                              
async function addClient(filename, clientData) {                
// Lê o arquivo e obtem o array atual:
const data = await readFile(filename, 'utf-8');
const json = JSON.parse(data || '[]');
                                                           
if (!Array.isArray(json)) {                                     
 throw new Error(`Malformed JSON. Expected array, got: ${json}.`);
}
                                                            
// Note que mutamos o JSON diretamente para "adicionar o cliente", uma vez que                                              
// sobre-escreveremos o arquivo com o novo conteúdo (modificado).
json.push(clientData);
                               
// Transformamos o JSON modificado em string para sobre-escrever no arquivo:
const jsonString = JSON.stringify(json);                                                                                    
// Modificamos o arquivo:
await writeFile(filename, jsonString);                     
}
                                                          
// Posso chamar quantas vezes for necessário, a estrutura sintática do JSON
// irá se manter válida.
addClient('mynewFile2.txt', {
 name: 'Bob',
 age: 10
})
.then(() => console.log('OK.'))
.catch(console.error);

This way the array stays inside an object, it is a good practice to do this way, I did not modify much the file that the colleague below gave the most important is the result, there is a question here in the stack overflow even to leave the array of the first example equal to the second, without changing the code so much, there is a question here in the stack overflow if you want to leave the first example equal to the second without many changes of a look. That’s the answer, vlw! :)

  • 1

    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.

  • It’s just an example, but because it’s not very valid?

  • 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.

  • 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. :-)

  • 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.

  • You can do something more or less thus.

  • 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.

  • Edited hehe :)

Show 3 more comments

-1

Talk, man, talk, talk, talk ?

Next, you really need to read this json file with the lib Fs ?

You can only import the file with (require)

This way you will have json already formatted on date and do not need parse.

But the way you did it will work too, only you have to parse it later like you did.

Then you need to set the values you want to modify, with "Document.clients[clientId]. score = new score"

For example :

const data = require('test.json');

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 //data.clients[clientId].score
    var client = data.clients[clientId].nome
    var valueOff = value - ((value * 25) / 100)

    if (value >= 150) {
        clientScore += 1;
        // você precisa setar o novo valor no objeto "score" antes de salvar 
        // ele no arquivo

        data.clients[clientId].score = clientScore;

        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()
    }
})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.