MySQL data is returned(promises is used) but EJS doesn't display the result in HTML page

Solution 1:

You can't use asynchronous code in EJS.

Your function goes to sleep when it hits the await, the EJS finishes, the result it sent to the browser, then the function wakes up and does the logging (by which time it is too late to inject content into the data that has already been sent to the browser).


Treat EJS is a view layer, and just as a view layer.

Collect all the data you need before it reaches EJS (typically you would do this in an Express.js route), put it into a sensible data structure, then pass it to the EJS template.

app.get('/path/example', async (req, res) => {
    const data = await getDataFromDatabase();
    res.render('/pages/example', {databaseData: data});
});