Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to seed the DB #868

Closed
aaryandewan opened this issue Sep 19, 2024 · 11 comments
Closed

Unable to seed the DB #868

aaryandewan opened this issue Sep 19, 2024 · 11 comments

Comments

@aaryandewan
Copy link

Been following this tutorial: https://nextjs.org/learn/dashboard-app/setting-up-your-database

When I'm trying to see the DB, it says:
image

I don't know what to do. I have tried everything.

My route.ts in my seed folder looks like:

image

My .env is also pretty good. it's set.

@aure1is
Copy link

aure1is commented Sep 19, 2024

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.

@aaryandewan
Copy link
Author

@arytix

Do this

  1. Make sure that you have uncommented out the seed/route.ts file
  2. You need to deploy your project on vercel.com
  3. Go to this url in your browser (whatever your deployed app's URL is obviously not this exact URL)
    image
  4. 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]

@aure1is
Copy link

aure1is commented Sep 19, 2024

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.

@aaryandewan
Copy link
Author

aaryandewan commented Sep 19, 2024

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

@aaryandewan aaryandewan reopened this Sep 19, 2024
@aure1is
Copy link

aure1is commented Sep 19, 2024

If you continue to chapter 7, where you start fetching data. It also won't work locally.
image

@aure1is
Copy link

aure1is commented Sep 19, 2024

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.

@sandeeppal007
Copy link

sandeeppal007 commented Sep 19, 2024

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 });
}
}

@siddhesh-k
Copy link

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

@Angeluz88
Copy link

Hello guys, the solution is as simple as deleting from line 105 to 108

@SteaneMurphy
Copy link

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.

@leerob
Copy link
Member

leerob commented Jan 27, 2025

#989

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants