N1lzh
N1lzh

Reputation: 47

element.join is not a function

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' ]

Upvotes: 0

Views: 193

Answers (3)

loom
loom

Reputation: 100

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

Upvotes: 0

Alaindeseine
Alaindeseine

Reputation: 4413

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();

Upvotes: 1

sean-7777
sean-7777

Reputation: 710

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

Upvotes: 1

Related Questions