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