next-learn
next-learn copied to clipboard
Unable to seed the DB
Been following this tutorial: https://nextjs.org/learn/dashboard-app/setting-up-your-database
When I'm trying to see the DB, it says:
I don't know what to do. I have tried everything.
My route.ts in my seed folder looks like:
My .env is also pretty good. it's set.
Same issue here, been trying this since yesterday. I commented everything out in the route.ts file except the import statements and the const client = await db.connect();
This statement raises the error shown above. So, there seems to be some sort of issue with the db object imported from @vercel/postgres.
I also tried multiple version of node, react, next etc. but always resulting in the same error. Interestengly it works when deployed and the seed page is opened on the deployed instance. But that doesn't help much as the forthcoming chapters rely on a local connection to the database. So fixing this issue is essential for the rest of the tutorial.
@arytix
Do this
- Make sure that you have uncommented out the seed/route.ts file
- You need to deploy your project on vercel.com
- Go to this url in your browser (whatever your deployed app's URL is obviously not this exact URL)
- Go to it's /seed. Do not go to localhost:3000/seed, instead go to "https://next-learn-o9ve.vercel.app/seed" [GO TO YOUR PROJECT's URL]
Yes, maybe if you read my comment completely you would see that futher down the road you still keep on having issues connecting to the database.
Oh sorry, didn't read it fully. I just copy pasted my answer from another issue. If you fix it, please tag me @arytix
Maybe keep on making changes and keep on pushing to github LOL
If you continue to chapter 7, where you start fetching data. It also won't work locally.
I got it working. I deleted everything and rebuilt from scratch. This time, I chose a server location for the database closer to my physical location instead of the default in Washington. Now, everything works perfectly. It seems there was a latency or distance restriction for a local connection to the DB.
paste this in route.ts update this push this code on githhub then go the
Domain URL go for the seed path /seed
import bcrypt from 'bcrypt'; import { db } from '@vercel/postgres'; import { invoices, customers, revenue, users } from '../lib/placeholder-data';
const client = await db.connect();
async function seedUsers() {
await client.sqlCREATE EXTENSION IF NOT EXISTS "uuid-ossp";
await client.sqlCREATE TABLE IF NOT EXISTS users ( id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, name VARCHAR(255) NOT NULL, email TEXT NOT NULL UNIQUE, password TEXT NOT NULL );;
const insertedUsers = await Promise.all(
users.map(async (user) => {
const hashedPassword = await bcrypt.hash(user.password, 10);
return client.sqlINSERT INTO users (id, name, email, password) VALUES (${user.id}, ${user.name}, ${user.email}, ${hashedPassword}) ON CONFLICT (id) DO NOTHING;;
}),
);
return insertedUsers; }
async function seedInvoices() {
await client.sqlCREATE EXTENSION IF NOT EXISTS "uuid-ossp";
await client.sqlCREATE TABLE IF NOT EXISTS invoices ( id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, customer_id UUID NOT NULL, amount INT NOT NULL, status VARCHAR(255) NOT NULL, date DATE NOT NULL );;
const insertedInvoices = await Promise.all(
invoices.map(
(invoice) => client.sqlINSERT INTO invoices (customer_id, amount, status, date) VALUES (${invoice.customer_id}, ${invoice.amount}, ${invoice.status}, ${invoice.date}) ON CONFLICT (id) DO NOTHING;,
),
);
return insertedInvoices; }
async function seedCustomers() {
await client.sqlCREATE EXTENSION IF NOT EXISTS "uuid-ossp";
await client.sqlCREATE TABLE IF NOT EXISTS customers ( id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, name VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, image_url VARCHAR(255) NOT NULL );;
const insertedCustomers = await Promise.all(
customers.map(
(customer) => client.sqlINSERT INTO customers (id, name, email, image_url) VALUES (${customer.id}, ${customer.name}, ${customer.email}, ${customer.image_url}) ON CONFLICT (id) DO NOTHING;,
),
);
return insertedCustomers; }
async function seedRevenue() {
await client.sqlCREATE TABLE IF NOT EXISTS revenue ( month VARCHAR(4) NOT NULL UNIQUE, revenue INT NOT NULL );;
const insertedRevenue = await Promise.all(
revenue.map(
(rev) => client.sqlINSERT INTO revenue (month, revenue) VALUES (${rev.month}, ${rev.revenue}) ON CONFLICT (month) DO NOTHING;,
),
);
return insertedRevenue; }
export async function GET() {
try {
await client.sqlBEGIN;
await seedUsers();
await seedCustomers();
await seedInvoices();
await seedRevenue();
await client.sqlCOMMIT;
return Response.json({ message: 'Database seeded successfully' });
} catch (error) {
await client.sqlROLLBACK;
return Response.json({ error }, { status: 500 });
}
}
After uncommenting the route.ts file remove this
return Response.json({
message:
'Uncomment this file and remove this line. You can delete this file when you are finished.',
});
from line 105 to 108 and run http://localhost:3000/seed this is enough to get you seeds load u[p in your db
Hello guys, the solution is as simple as deleting from line 105 to 108
The issue as far as I can tell is that the Washington default server causes a latency that lags out the request. If you are using the "@vercel/postgres" package to connect to their cloud database, it might work better if you delete the database, re-create the database, and select a server close to your geographic location.
One of the ways I confirmed this was the issue:
- used the cloud deployed site with the /seed endpoint, this successfully seeded the database (Washington server)
- created a new database with a geographically close server to me, this also worked from the deployed /seed endpoint (Closer server)
- the new database also worked locally when using the local /seed endpoint (Closer server)
Without looking into the specific code that is a part of the Vercel postgres package, I am going to assume that the request is timing out (read: unresolved promise) due to geographic location. This issue should be fixed if someone from Vercel ever has 20 minutes to fix this.
https://github.com/vercel/next-learn/pull/989