En este comando de search
, el bucle for in
no agrega los campos a la inserción. Cuando uso el comando, obtengo el nombre y la descripción, que definí al crear la inserción. Pero no obtengo los campos que agrego en el bucle for in
.
Aquí hay una imagen de lo que se devuelve, en caso de que ayude a responder mi pregunta:
Mi código de comando de search
:
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]}) }, };
Debe usar embed.addField(...)
o pasar una matriz a embed.addFields([...])
.
<MessageEmbed>.addFields()
toma una matriz como entrada.
La solución: Usar for of
en lugar de for in
Diferencia entre los 2:
La instrucción for...of crea un bucle que itera sobre objetos iterables , incluidos: String
, Array
, objetos similares a matrices (p. ej., argumentos o NodeList
), TypedArray
, Map
, Set
La instrucción for...in itera sobre todas las propiedades enumerables de un objeto que están tecleadas por cadenas (ignorando las tecleadas por símbolos), incluidas las propiedades enumerables heredadas.
Para más información:
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