I have my index.js like this
var express = require('express');
var app = express();
var list = require('./data').list;
// [...]
app.get('/', function (req, res) {
res.render('home',{
list: list,
});
});
and data.js that should ask the database server for data
const { Pool, Client } = require('pg')
var list = [];
const pool = new Pool({
connectionString: '...',
})
pool.query('select * from list', (err, res) => {
if(res){
list = res.rows;
}
pool.end()
})
exports.list = list;
The problem is that when the page is rendered the list is empty and the database call happens after the page render. Is there a way to avoid this?
listwill be available when the request to/is made. First make sure your query has finished executing before the request is made.