N
Neon2y ago
rare-sapphire

Can't get updated data after update data in production

hello, im using next js with prisma orm and SWR with axios for fetching from client and free tier neon database in my next js app, after updating the data and revalidate the data using swr, the data that i get is old data, this only happen in production mode even the database data is updated. but if in dev mode, its updated succesfully and always get new data this is what i already tried: - increasing or disable connection timeout and pool timeout - using pooled connection and direct connection url any suggestions?
7 Replies
itchy-amethyst
itchy-amethyst2y ago
Can you explain what dev mode and production mode are? Are you using branching within Neon? Have you tried using psql or the Neon SQL Editor to make the same query
rare-sapphire
rare-sapphireOP2y ago
Can you explain what dev mode and production mode are? dev mode is running on localhost using next dev production mode is using next build then running next start in localhost, or running on vercel Are you using branching within Neon? no Have you tried using psql or the Neon SQL Editor to make the same query i'm using prisma studio and neon sql editor, the data its updated while update the data
like-gold
like-gold2y ago
I've seen similar behaviour reported before. In those instances, the local deployment was falling back to a local postgres instance -- so it was writing local, vs the cloud. You can test this by performing a write in the application, then running a query on the console to see if the data is there. You can try the reverse by writing using a query at the console, then pulling a local read. Try simplifying your test harness:
const { Pool } = require('pg');
require('dotenv').config();

// Replace these values with your PostgreSQL connection details
const connectionString = process.env.DATABASE_URL;
const pool = new Pool({ connectionString });

// Create a table
async function createTable() {
const client = await pool.connect();
try {
await client.query(`
CREATE TABLE IF NOT EXISTS example_table (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
age INT
);
`);
console.log('Table created successfully');
} finally {
client.release();
}
}

// Insert some records
async function insertRecords() {
const client = await pool.connect();
try {
const insertQuery = `
INSERT INTO example_table (name, age)
VALUES ($1, $2), ($3, $4);
`;
const values = ['John Doe', 25, 'Jane Doe', 30];
await client.query(insertQuery, values);
console.log('Records inserted successfully');
} finally {
client.release();
}
}

// Connect to PostgreSQL and execute operations
async function main() {
try {
await createTable();
await insertRecords();
} catch (error) {
console.error('Error:', error);
} finally {
// Close the database connection pool
await pool.end();
}
}

// Run the main function
main();
const { Pool } = require('pg');
require('dotenv').config();

// Replace these values with your PostgreSQL connection details
const connectionString = process.env.DATABASE_URL;
const pool = new Pool({ connectionString });

// Create a table
async function createTable() {
const client = await pool.connect();
try {
await client.query(`
CREATE TABLE IF NOT EXISTS example_table (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
age INT
);
`);
console.log('Table created successfully');
} finally {
client.release();
}
}

// Insert some records
async function insertRecords() {
const client = await pool.connect();
try {
const insertQuery = `
INSERT INTO example_table (name, age)
VALUES ($1, $2), ($3, $4);
`;
const values = ['John Doe', 25, 'Jane Doe', 30];
await client.query(insertQuery, values);
console.log('Records inserted successfully');
} finally {
client.release();
}
}

// Connect to PostgreSQL and execute operations
async function main() {
try {
await createTable();
await insertRecords();
} catch (error) {
console.error('Error:', error);
} finally {
// Close the database connection pool
await pool.end();
}
}

// Run the main function
main();
rare-sapphire
rare-sapphireOP2y ago
Oh, on my dev mode i setup the database url is connecting to the neon database, so im sure its not connect to the local db So, at the dev mode or production mode, im using same neon db url
constant-blue
constant-blue2y ago
@Daniel Tan you say this only happens in production, and you're sure the database is updated? In that case this would suggest it's an issue with SWR/Next,js right? SWR does perform caching AFAIK, so perhaps that's what's happening? Check the Network inspector in DevTools to see SWR is actually fetching new data from the backend, and make sure that it's what you expect. I'm not an expert in Next.js but you might also need export const dynamic = 'force-dynamic' in your API route to make sure it always performs a query aginst the db?
rare-sapphire
rare-sapphireOP2y ago
ok, i will find out about it update: after i add
export const dynamic = 'force-dynamic'
export const dynamic = 'force-dynamic'
its finally working so its because next js caching thanks for the help
constant-blue
constant-blue2y ago
Youre welcome!

Did you find this page helpful?