I want to reform the function router.get('/', etc) to one that uses async and await. The res.send and connection.query are making the trouble I don't know how to handle them using async/await constructs.
I tried to use this function:
const awaitHandlerFactory = (middleware) => {
return async (req, res, next) => {
try {
await middleware(req, res, next)
} catch (err) {
next(err)
}
}
};
This is the code that i have in the file:
var express = require('express');
// const data = require("../data/repo");
var router = express.Router();
let mysql = require("mysql");
let config = require('../config/config.json');
let connection = mysql.createConnection(config);
connection.connect();
//
let guests = {};
let query1 = "select * from guests";
//GET home page.
router.get('/', function(req, res, next) {
connection.query(query1, function (err, rows) {
guests['guests'] = rows;
res.send(guests)
});
});
this function works the router.get('/', etc...)
I get this: and this is what i want.
"guests": [
{
"event_id": 1,
"guest_id": 1,
"firstName": "lkasjdl;skj",
"lastName": "lskajdfal;skdj",
"role": "lsakdjfalskdjflks;adjf",
"created_at": null,
"updated_at": null
},