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