Node.js expressmysql-api需要帮助返回查询

Node.js expressmysql-api需要帮助返回查询,node.js,express,Node.js,Express,我有一个express api,它通过get请求返回JSON数据。它从MySQL数据库获取数据 当我返回一个示例数据JSON时,它工作正常。但是,我对如何从MySQL返回查询数据感到困惑 我知道我正在获取数据,因为我使用了console.log(行[0]),它会打印我想要的数据,但我不知道如何发送它 谢谢你的帮助 /* GET users listing. */ router.get('/2018-2017/2', function(req, res, next) { connection.

我有一个express api,它通过get请求返回JSON数据。它从MySQL数据库获取数据

当我返回一个示例数据JSON时,它工作正常。但是,我对如何从MySQL返回查询数据感到困惑

我知道我正在获取数据,因为我使用了console.log(行[0]),它会打印我想要的数据,但我不知道如何发送它

谢谢你的帮助

/* GET users listing. */
router.get('/2018-2017/2', function(req, res, next) {


connection.query('CALL BRWally.spGetDealerships()', function (err, 
rows, fields) {
 if (err) throw err

  console.log('The solution is: ', rows[0])
})

/* rows[0] contains the data i want to return.

I am unsure how to send it.

To send static JSON data i have done...

res.json(
  [{
  id: 1,
  week: "15",
  year: "2016-2017",

  }

]);

*/
res.send({data: rows[0]});
});


/* terminal output */
You are now connected...
GET /users/2018-2017/2 404 15.324 ms - 156
The solution is:  [ RowDataPacket { PK_DealershipName: 'Guelph             
Auto Mall' },
  RowDataPacket { PK_DealershipName: 'Sports World' },
  RowDataPacket { PK_DealershipName: 'Waterloo' } ]
您只需将“send”移动到查询函数回调内部

router.get('/2018-2017/2', function(req, res, next) {
  connection.query('CALL BRWally.spGetDealerships()', function (err,   rows, fields) {
    if (err) throw err
    res.send({data: rows[0]});
    console.log('The solution is: ', rows[0])
  })
});
可能重复的