刚开始直接使用ctx.body直接赋值的方式,发现直接GG了,找了资料才发现不能直接操作
const fs = require('fs');
const router = require('koa-router')();
const mysql = require('mysql');
const config = require('../config');
const connection = mysql.createConnection(config.db_set);
connection.connect(function (err) {
if (err) {
console.log(err)
}
});
var getInformations = 'select * from v_position';
router.get('/getInformations', async (ctx, next) => {
await next();
ctx.body = result;
})
而是需要使用promise封装才可以
router.get('/getInformations', async (ctx, next) => {
let query = ()=>{
return new Promise((resolve,reject)=>{
connection.query(getInformations,(err,data) => {
if(err){
resolve({
message:err.message
})
}
resolve(data);
})
})
}
let result = await query();
ctx.body = result;
})