I am using PostgreSQL with Express.js
I came across a problem, that when I update my database, with new entries, and delete old ones, no matter what, it keeps returning only the old ones (that were deleted several days ago already), as if it was using cache.
I tried:
Setting headers - res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, private');
Restarting the database itself, and using VACUUM ANALYZE on PostgreSQL;
This is my connection to the database:
const { Pool } = require('pg');
const { dbConfig, sshConfig } = require('./database_config');
const dbPool = new Pool(dbConfig);
const connectToDatabase = () => {
dbPool.connect((error) => {
if (error) throw error;
});
};
module.exports = { dbPool, connectToDatabase };
This is one of my endpoints.
//I connect to the database in index.js
connectToDatabase();
app.get('/getEntries', async (req, res) => {
try {
const query = 'SELECT * FROM listings';
const result = await dbPool.query(query);
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, private');
res.json(result.rows);
} catch (error) {
console.error('Error fetching data:', error);
res.status(500).send('Error fetching data');
}
});
It keeps returning old data, deleted a while ago from the Database, how to address this issue?
from Express.js keeps returning old query results that no longer exist in the database
No comments:
Post a Comment