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

98
Vistas
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
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