How not to repeat information in a js Node file

Asked

Viewed 47 times

-2

You guys, I got this comic:

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

Note that every time I run the comic it will repeat the information, but I want you not to repeat the same information, but I want to achieve this result in a simple way without changing much in the comic if possible. It is that I was answering a person here in stack overflow and I gave this answer and a comment made me see this problem, but I use the appendFile and I wonder if there’s any way to do that without needing to almost rewrite the entire comic

  • What do you mean by "repeat information"? Always save the same data?

  • Yes, it was this question that I was referring to, I would like to know if there is how not to repeat data (or duplicate information) without needing to change the code a lot, as in that example that you pointed out in another question.

  • Is that I use the appendFile quite ... And that left me curious should kk.

  • You came to look at the code I posted as a comment on your reply? See you again here. Note that it does not overwrite the data, only adds a new element at the end of the array... Replacing the file contents (with a new, incremented one) is different from replacing the information.

  • I tried to run over returned me an error in import, and even if I use require to import the modules etc, it returns me another error in JSON.parse(data?.trim() | | '[]') I couldn’t execute the code to better understand, so I asked the other question.

  • Must be my version of Node js I believe

  • That worked out hehe I will edit the answer, I had understood something else kkk I spent a long time trying to solve that.

Show 3 more comments

1 answer

1


One way to solve the problem is to not use the appendFile.

Using your code, simply modify the appendFile for the function writeFile

Follow an example:

const fs = require('fs');
let data = null
try {
    data = JSON.parse(fs.readFileSync('./mynewfile2.txt'))
} catch (e) {
    console.log('não foi possível ler o arquivo.')
}

const produto = {
 nome: 'Smartphone',
 preco: 1749.99,
 descontoo: 0.15
}
const produtos = data || []
produtos.push(produto)
fs.writeFile('mynewfile2.txt', JSON.stringify(produtos), err => {
  console.log(err || 'Arquivo salvo');
});

Note that in the code I just changed the name of the function used in the module fs. Follows the documentation

When using the function appendFile you are always writing data at the end of the file. By using writeFile we are writing over the previously recorded information.

In this code we are using the try catch only to ensure the execution of the code successfully when the file does not exist.

Using this logic note that each time we run the code we are adding a new entity to the product array.

  • Yes it would solve the problem, only that I would like not replace information that are already there draws? The appendFile does not replace, if I use the writeFile it will overwrite the entire file even if the information that is there is not repeated.

  • Now I understand the requirement, follow the correction.

Browser other questions tagged

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