Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

discord.js MessageEmbed fields.flat is not a function

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)
like image 794
milkwood1 Avatar asked Mar 30 '20 16:03

milkwood1


1 Answers

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.

like image 194
slothiful Avatar answered Oct 30 '22 00:10

slothiful