I'm setting up a standard forum application and have the route set up to retrieve data from the database and send it over.app .route('/get-threads') .options((req: express.Request, res: express.Response) => { res.setHeader('Access-Control-Allow-Origin', app.get('host')); // "http://localhost:3000" res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); res.status(200).end(); }) .get((req: express.Request, res: express.Response) => { res.type('application/json'); res.setHeader('Access-Control-Allow-Origin', app.get('host')); res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); res.setHeader('Access-Control-Allow-Methods', 'GET,OPTIONS'); res.send(connection.getRepository(Threads).find()); // this retrieves all rows from the 'threads' table }); However, as you can imagine, it doesn't get to send the data because the res.send() part is executed before the data can be fully retrieved. So the solution is to use async/await to ensure the data is retrieved before sending it over, right? I can't do that with Express as it is, as far as I know. So is there a way to ensure res.send() actually sends the data when the data is 100% retrieved or do I need to migrate the whole thing to Koa with its built-in async/await functionality?
Submitted April 17, 2019 at 04:38PM by beefyjon
No comments:
Post a Comment