Home > Enterprise >  element.join is not a function
element.join is not a function

Time:06-06

I'm currently trying to write a bunch of IDs into an external file. I create the File with the nodejs file system and also write into it using it. The file is created but the bot crashes with the following error as soon as an attempt is made to write to the file TypeError: element.join is not a function. A short version of my code looks like this:

const fs = require("fs");

let gameChannel = interaction.member.voice.channel;
let memberIDs = gameChannel.members.map(member => member.id);

let file = fs.createWriteStream("Sessions/"   sessionID   ".txt");
memberIDs.forEach(element => {file.write(element.join(", "))});
file.end();

The Array looks like this: [ '392776445375545355', '849388169614196737' ]

CodePudding user response:

If memberIDs contain [ '392776445375545355', '849388169614196737' ] You are doing the job twise, try this instead:

const fs = require("fs");

let gameChannel = interaction.member.voice.channel;
let memberIDs = gameChannel.members.map(member => member.id);

let file = fs.createWriteStream("Sessions/"   sessionID   ".txt");
file.write(memberIDs.join(", "));
file.end();

CodePudding user response:

You are looping through the array, so element is a string. Try doing file.write(memberIDs.join(", ")).

CodePudding user response:

element refers to the string you get when you loop through every string of the array. You are trying to join a string with nothing else. What you could do is this:

const fs = require("fs");

let gameChannel = interaction.member.voice.channel;
let memberIDs = gameChannel.members.map(member => member.id);

let file = await fs.createWriteStream("Sessions/"   sessionID   ".txt");
await file.write(memberIDs.join(", "));
file.end();

This way you open the file, loop through every element of the array and join them with , then write to file, and then close the file

  • Related