Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

104
Vistas
I have a problem while trying to use forEach

I am trying, when someone writes a message in guild A/B/C, the bot who is in for example 3 servers, to send me the message in the 4th server, sending them in different text channels named "guild A", "guild B" and " guild C ", not in the same ones...

I get the following error:

channel.send(msgLog)

TypeError: Cannot read property 'send' of undefined

And this is my code:

const msgLog = `[#${message.channel.name}]=[${message.author.username}#${message.author.discriminator}]=> ${message.content}` ```

client.guilds.cache.map(guild => server.channels.cache.find(channel => channel.name == guild.name)).forEach(channel => 
      channel.send(msgLog)
      );
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The error means that channel is undefined, and you cannot any property (such as 'send') of undefined.

This means that server does not have a channel with the name guild.name for some guilds.

You could use filter to only include channels that are defined:

client.guilds.cache
  .map(guild => server.channels.cache.find(channel => channel.name == guild.name))
  .filter(channel => channel) // returns false if channel === undefined
  .forEach(channel => channel.send(msgLog));

This is roughly equivalent to

client.guilds.cache.forEach(guild => {
  const channel = server.channels.cache.find(channel => channel.name == guild.name));
  if (channel) {
    channel.send(msgLog);
  }
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda