Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

223
Visualizações
Fs incorrectly reads or saves file leading to NaN. How to deal with it?

I'm using Fs to save some data in a file. I start by having 0 in the first line and 0 in the second line. Then I want to add by one the second line. And sometimes after some tries (random, sometimes 100 and sometimes 700) something happens leading to first empty line and NaN in the second one. The code:

const Discord = require("discord.js");

const fs = require('fs');

module.exports = {
  name: "testkom",
  aliases: [],
  async execute(message, args, client) {
    
      var data = fs.readFileSync('peakcommands.txt').toString().split("\n");
      
      data[1]++;
      var text = data.join("\n");



      fs.writeFile('peakcommands.txt', text, function (err) {
      if (err) return console.log(err);
      });


      message.channel.send (data[1]);

  }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think it could either be an encoding issue or possibly a data race.

To fix the possible race condition, switch fs.writeFile to fs.writeFileSync, which will assure that the file write is synchronous.

To fix the encoding issue, you can specify the encoding, like this:

fs.writeFileSync('peakcommands.txt', 'utf8' text, function (err) {
    if (err) return console.log(err);
});

If nothing works, check the docs.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda