11
votes

I am making a Discord bot using JavaScript and discord.js. There, I want to send a RichEmbed/MessageEmbed (I don't know how it's called) to a channel. Instead of sending an Embed though, it threw an error inside discord.js.

TypeError: fields.flat is not a function
    at Function.normalizeFields (D:\discord-bot\node_modules\discord.js\src\structures\MessageEmbed.js:436:8)
    at MessageEmbed.addFields (D:\discord-bot\node_modules\discord.js\src\structures\MessageEmbed.js:252:42)
    at commands.forEach.command (D:\discord-bot\src\js\core\commands\commandManager.js:55:19)
    at Array.forEach (<anonymous>)
    at helloWorldEmbed (D:\discord-bot\src\js\core\commands\commandManager.js:54:18)
    at Object.call (D:\discord-bot\src\js\core\commands\commandManager.js:29:13)
    at Client.client.on (D:\discord-bot\src\js\core\bot.js:16:49)
    at Client.emit (events.js:182:13)
    at MessageCreateAction.handle (D:\discord-bot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (D:\discord-bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)

I searched already for an answer, but it seems like I'm the only person having trouble with it.

Here's the code I used:

const embed = new MessageEmbed()
    .setTitle('Hello World')
    .setDescription('This is a test.')
    .setColor('#3498db')
quotes.forEach(quote => {
    embed.addField(quote.name, quote.description, true)
})
message.channel.send('Hello world.', embed)
1
You're problem isn't originating in the code you provided. Are you taking an existing embed and trying to remove the fields anywhere? I suspect you are setting the fields to null or undefined, instead of an empty array.Tarazed
@Tarazed after posting that message, I don't do anything with that embed anymoremilkwood1
Ok, well what the error message is saying is that embed.fields.flat is not a function, which means embed.fields is not the array that it should be (arrays have a built in flat function). Something is changing it's type, somewhere. It's not in this code, the constructor creates the embed with an empty array in fields and I don't see anywhere you change that.Tarazed
@Tarazed ok I went into the MessageEmbed.js file and logged the fields parameter and whether it's an instanceof Array. It is indeed an arraymilkwood1
okay I checked my node.js version and it seems quite outdated, probably that's the problemmilkwood1

1 Answers

16
votes

As discussed in comments, updating Node.js solves the issue. Discord.js v12 requires 12.0.0 or newer because of the methods (like Array#flat() in the error) it uses for efficiency which don't exist in older versions.