Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

271
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda