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

273
Visualizações
How can I check if a file exists or not to delete it and create it?

Hello I have an error as it does not find a file but I do not understand, if someone could help me =)

if (choice == "txt") {
            const dataWhitelist = await whitelists.find({ 'guildId': interaction.guild.id });
            const nameFile = interaction.guild.id + '.txt';
            if (fs.existsSync(`./tmp/${nameFile}`)) {
                fs.unlinkSync(`./tmp/${nameFile}`)
            }
            for (const i in dataWhitelist) {
                fs.appendFile(`./tmp/${nameFile}`, dataWhitelist[i].userId + ',' + dataWhitelist[i].address + ';', function (err) {
                    if (err) {
                        console.log(err)
                    } else {
                        // done
                    }
                })
            }
            interaction.deferReply({ ephemeral: true });
            interaction.followUp({ content: `Here is the export for your server.`, files: [{ attachment: `./tmp/${nameFile}` }] });
        }

and this is the error I get when I make my commands

node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

[Error: ENOENT: no such file or directory, stat '/home/user/bots/whitelister_mongoose/tmp/859055688965816320.txt'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'stat',
  path: '/home/user/bots/whitelister_mongoose/tmp/859055688965816320.txt'
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Agree with CherryDT that the error you got is probably due to parallel appending to the same file multiple times. You can try the asynchronous version below:

if (choice == "txt") {
  const dataWhitelist = await whitelists.find({ 'guildId': interaction.guild.id });
  const nameFile = interaction.guild.id + '.txt';
  try {
    await fs.promises.unlink(`./tmp/${nameFile}`);
    for await (const i in dataWhitelist) {
      await fs.promises.appendFile(`./tmp/${nameFile}`, dataWhitelist[i].userId + ',' + dataWhitelist[i].address + ';');
    }
  } catch(err) {
    console.log(err);
  }
  interaction.deferReply({ ephemeral: true });
  interaction.followUp({ content: `Here is the export for your server.`, files: [{ attachment: `./tmp/${nameFile}` }] });
}
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