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

132
Visualizações
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 à 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