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

96
Views
Lambda callback behaving differently than named callback in API call

I have the following code in a discord.js application.

class Bot {
    // ...

    activate() {
        this.client.login(this.token)
        this.client.on('messageCreate', this.handleMessage) // relevant
    }
    handleMessage(message: any) {
        if (message.mentions.has(this.client.user)) // relevant
            message.reply("hello")
    }
}

When I run and ping the bot on discord, node raises this error

src/Bot.ts:26
        if (message.mentions.has(this.client.user))
                                              ^
TypeError: Cannot read properties of undefined (reading 'user')

If I console.log(this.client.user), I see that it returns null

However, the code works if I replace the this.handleMessage callback with a lambda function containing all the executions.

    activate() {
        this.client.login(this.token)
        this.client.on('messageCreate', (message) => {
            if (message.mentions.has(this.client.user)) {
                message.reply('hello')
        })
    }
    // removed 'handleMessage()'

console logging the user shows an actual object and the bot replies.

Why does the anonymous lambda work but passing a predefined callback does not?

about 4 years ago · Juan Pablo Isaza
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!