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

131
Vistas
Using a method from a 'this' object in an express post function

I have a class with two methods:

var express = require('express');

class TestClass{
    constructor() {}

    first() {
        var app = express();
        var path = require('path');
        app.use( express.urlencoded({ extended: true }) )
        app.use( express.json() );
    
        app.post('/centroid', function(req, res) {
            this.second(req.body);
        });

        app.listen(4444);
    }
    second(data) { console.log(data) }
}


module.exports = TestClass;

When I try to reference the second method while using this, I get an error that this is undefined.

I have looked into .bind() method, but it seems that I can't use that with passing the req.body argument. Because it would require to bind it in the callback of the .post function.

How can i resolve this and use second() method in a callback of a post function?

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