Why do I get an empty object when a function is invoked in node.js?

后端 未结 1 1399
[愿得一人]
[愿得一人] 2020-12-12 06:08

I have this app.get in my node.js Express Server.

  app.get(\'/api/court/:num\', function(req, res, next) {
    var courts = new C         


        
相关标签:
1条回答
  • 2020-12-12 07:02

    Your courtsAmount doesn't return anything. Instead you should use a callback inside it (or a promise), to do something like this:

    this.courtsAmount = function(callback){
    connection.query('SELECT COUNT(*) AS result from courts', function(err, rows, fields){
        if (err) throw err;
        connection.end();
        console.log(rows[0].result);
        callback(rows[0].result);
        });
      };
    

    And

    app.get('/api/court/:num', function(req, res, next) {
     var courts = new CourtsHandler;
     if (req.params.num == 0) //get array of all courts
       courts.courtsAmount(function(result) { res.send(200, result) });
    });
    
    0 讨论(0)
提交回复
热议问题