Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

96
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda