Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

95
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!