app.get
我的node.js
Express 服务器中有这个。
app.get('/api/court/:num', function(req, res, next) {
var courts = new CourtsHandler;
if (req.params.num == 0) //get array of all courts
return res.send(200, courts.courtsAmount());
});
这是调用这个函数:
this.courtsAmount = function(){
connection.query('SELECT COUNT(*) AS result from courts', function(err, rows, fields){
if (err) throw err;
connection.end();
console.log(rows[0].result);
return rows[0].result;
});
};
courtAmount 函数被调用。但在我的客户看来,我没有得到结果。相反,我只是得到一个空对象。
我认为这与 my 有一个回调的事实有关.query
,因此在实际触发res.send
之前发送了一个空对象。courtsAmount
我该如何解决这个问题?