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

115
Visualizações
Express return value from method with callback

Why does this

router.get('/eventTest/:id', function (req, res) {

var event = Event.getEventById(req.params.id, function (err, rows) {
    if(err) {
        return err;
    } else {
        return rows;
    }
});

res.json(event);

});

returns { "domain": null, "_events": {}, "_eventsCount": 0, "_callSite": {}, "_ended": false, "_idleNext": null, "_idlePrev": null, "_idleStart": null, "_idleTimeout": -1, "_repeat": null, "sql": "select * from event where id=?", "values": [ "1" ], "typeCast": true, "nestTables": false, "_resultSet": null, "_results": [], "_fields": [], "_index": 0, "_loadError": null }

this and example of one of the methods that I'm calling

`getEventById:function(id,callback){

    return db.query("select * from event where id=?",[id],callback);
}

`

rather than the actual values like it does when I use res.json(rows) inside the Event.getEventById callback?

I need this in order to call multiple methods and add them to an object and then send that object in JSON

Thank you

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

As per your code, yes you need to send response from method.

Since as Node.js nature is async so you need to write your code in that way

Try it in your route

Event.getEventById(req.params.id, function (err, rows) {
    if(err) {
        res.status(500).send(err);
    } else {
      res.status(200).send(rows);
    }
});

UPDATE

To execute multiple methods and send all records at single time then go through

async module or promise

about 4 years ago · Santiago Trujillo Relatório

0

I tried "express-promise" like this:

`router.get('/eventTest/:id', function (req, res) {
    res.json({
       details: Event.getEventById(req.params.id),
       stages: Event.getEventStages(req.params.id)
    });
 });`

and still got the same result (i.e. the query and not the values). I'm guessing that my problem is the way that I managing the callbacks from the methods. Is this right?

about 4 years ago · Santiago Trujillo Relatório

0

router.get('/eventTest/:id', function (req, res, next) {

  Event.getEventById(req.params.id, function (err, rows) {
    if(err) 
        return next(err);

    res.json(rows);
  });
});
about 4 years ago · Santiago Trujillo Relatório
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