Search code examples
javascriptnode.jsdiscord.jsdistube

The embed in my search command isn't working properly


In this search command, the for in loop is not adding the fields to the embed. When I use the command, I get the name and description, which I defined while creating the embed. But I do not get the fields which I add in the for in loop.

Here is an image of what is returned, just in case it helps answer my question:

An image of what the command returns

My search command code:

const { MessageEmbed } = require('discord.js')

module.exports = {
    name: 'search',
    description: 'Searches for a song and returns the results.',
  options: [
    {
      name: 'search_term',
      description: 'The song to search for.',
      type: 'STRING',
      required: true
    },
    {
      name: 'limit',
      type: 'INTEGER',
      description: 'Number of results to return.',
      required: true
    },
    {
      name: 'type',
      type: 'STRING',
      description: 'The type of search result.',
      required: true,
      choices: [{
        name: 'Video',
        value: 'video'
      },
      {
        name: 'Playlist',
        value: 'playlist'
      }]
    }
  ],
    async execute(interaction) {
    const query = await interaction.options.getString('search_term')
    const limit = await interaction.options.getInteger('limit')
    const type = await interaction.options.getString('type')

    let type2 = 'x'
    if (type === 'video') type2 = 'Video'
    else if (type === 'playlist') type2 = 'Playlist'

    let results = await interaction.client.distube.search(query, { limit: limit, type: type })

    let embed = new MessageEmbed()
    .setTitle('search')
    .setDescription('Searches for a song and returns the results.')

    for (const result in results) {
      if (result.type === 'video') {
        embed.addFields({
          name :result.name, 
          value: `ID: ${result.id}\nType: ${type2}\nURL: ${result.url}\nViews: ${result.views}\nDuration: ${result.formattedDuration}\nLive: ${result.isLive}`, 
          inline: true})
      }
      else if (result.type === 'playlist') {
        embed.addFields({
          name: result.name, 
          value: `ID: ${result.id}\nType: ${type2}\nURL: ${result.url}\nViews: ${result.views}`,
          inline: true})
      }
    }

    await interaction.reply({embeds: [embed]})
    

    },
};

Solution

  • The solution: Use for of instead of for in

    Difference between the 2:

    The for...of statement creates a loop iterating over iterable objects, including: built-in String, Array, array-like objects (e.g., arguments or NodeList), TypedArray, Map, Set

    The for...in statement iterates over all enumerable properties of an object that are keyed by strings (ignoring ones keyed by Symbols), including inherited enumerable properties.

    For more information:

    for of: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of

    for in: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in