Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

270
Views
¿Cómo puedo comprobar si un archivo existe o no para borrarlo y crearlo?

Hola tengo un error ya que no encuentra un archivo pero no entiendo, si alguien me pudiera ayudar =)

 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}` }] }); }

y este es el error que me sale cuando hago mis comandos

 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 answers
Answer question

0

De acuerdo con CherryDT en que el error que recibió probablemente se deba a la adición paralela al mismo archivo varias veces. Puede probar la versión asíncrona a continuación:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!