Open J3020605821 opened 8 months ago
everyone facing the same error
Does anyone know how to link to a local database
try running vercel dev instead of npm run dev
that did the magic for me https://github.com/orgs/vercel/discussions/2350#discussioncomment-5850746
After running Vercel dev, there is still an error: Failed to fetch user: NeonDbError: Error connecting to database: fetch failed
I face the same problem. Have you solved this problem? For anyone who have solved this problem, please share the solution
how did you fix this issue?
`import postgres from 'postgres';
import {
CustomerField,
CustomersTableType,
InvoiceForm,
InvoicesTable,
LatestInvoiceRaw,
Revenue,
} from './definitions';
import { formatCurrency } from './utils';
// Initialize the postgres client
const sql = postgres(`${process.env.POSTGRES_URL}`);
export async function fetchRevenue() {
try {
console.log('Fetching revenue data...');
const data = await sql<Revenue[]>`SELECT * FROM revenue`;
console.log('Data fetch completed.');
return data;
} catch (error) {
console.error('Database Error:', error);
throw new Error('Failed to fetch revenue data.');
}
}
export async function fetchLatestInvoices() {
try {
const data = await sql<LatestInvoiceRaw[]>`
SELECT invoices.amount, customers.name, customers.image_url, customers.email, invoices.id
FROM invoices
JOIN customers ON invoices.customer_id = customers.id
ORDER BY invoices.date DESC
LIMIT 5`;
const latestInvoices = data.map((invoice) => ({
...invoice,
amount: formatCurrency(invoice.amount),
}));
return latestInvoices;
} catch (error) {
console.error('Database Error:', error);
throw new Error('Failed to fetch the latest invoices.');
}
}
export async function fetchCardData() {
try {
const invoiceCountPromise = sql`SELECT COUNT(*) FROM invoices`;
const customerCountPromise = sql`SELECT COUNT(*) FROM customers`;
const invoiceStatusPromise = sql`
SELECT
SUM(CASE WHEN status = 'paid' THEN amount ELSE 0 END) AS "paid",
SUM(CASE WHEN status = 'pending' THEN amount ELSE 0 END) AS "pending"
FROM invoices`;
const [invoiceCountResult, customerCountResult, invoiceStatusResult] = await Promise.all([
invoiceCountPromise,
customerCountPromise,
invoiceStatusPromise,
]);
const numberOfInvoices = Number(invoiceCountResult[0].count ?? '0');
const numberOfCustomers = Number(customerCountResult[0].count ?? '0');
const totalPaidInvoices = formatCurrency(invoiceStatusResult[0].paid ?? '0');
const totalPendingInvoices = formatCurrency(invoiceStatusResult[0].pending ?? '0');
return {
numberOfCustomers,
numberOfInvoices,
totalPaidInvoices,
totalPendingInvoices,
};
} catch (error) {
console.error('Database Error:', error);
throw new Error('Failed to fetch card data.');
}
}
const ITEMS_PER_PAGE = 6;
export async function fetchFilteredInvoices(query: string, currentPage: number) {
const offset = (currentPage - 1) * ITEMS_PER_PAGE;
try {
const invoices = await sql<InvoicesTable[]>`
SELECT
invoices.id,
invoices.amount,
invoices.date,
invoices.status,
customers.name,
customers.email,
customers.image_url
FROM invoices
JOIN customers ON invoices.customer_id = customers.id
WHERE
customers.name ILIKE ${'%' + query + '%'} OR
customers.email ILIKE ${'%' + query + '%'} OR
invoices.amount::text ILIKE ${'%' + query + '%'} OR
invoices.date::text ILIKE ${'%' + query + '%'} OR
invoices.status ILIKE ${'%' + query + '%'}
ORDER BY invoices.date DESC
LIMIT ${ITEMS_PER_PAGE} OFFSET ${offset}
`;
return invoices;
} catch (error) {
console.error('Database Error:', error);
throw new Error('Failed to fetch invoices.');
}
}
export async function fetchInvoicesPages(query: string) {
try {
const count = await sql`
SELECT COUNT(*)
FROM invoices
JOIN customers ON invoices.customer_id = customers.id
WHERE
customers.name ILIKE ${'%' + query + '%'} OR
customers.email ILIKE ${'%' + query + '%'} OR
invoices.amount::text ILIKE ${'%' + query + '%'} OR
invoices.date::text ILIKE ${'%' + query + '%'} OR
invoices.status ILIKE ${'%' + query + '%'}
`;
const totalPages = Math.ceil(Number(count[0].count) / ITEMS_PER_PAGE);
return totalPages;
} catch (error) {
console.error('Database Error:', error);
throw new Error('Failed to fetch total number of invoices.');
}
}
export async function fetchInvoiceById(id: string) {
try {
const data = await sql<InvoiceForm[]>`
SELECT
invoices.id,
invoices.customer_id,
invoices.amount,
invoices.status
FROM invoices
WHERE invoices.id = ${id};
`;
const invoice = data.map((invoice) => ({
...invoice,
amount: invoice.amount / 100, // Convert amount from cents to dollars
}));
return invoice[0];
} catch (error) {
console.error('Database Error:', error);
throw new Error('Failed to fetch invoice.');
}
}
export async function fetchCustomers() {
try {
const customers = await sql<CustomerField[]>`
SELECT
id,
name
FROM customers
ORDER BY name ASC
`;
return customers;
} catch (err) {
console.error('Database Error:', err);
throw new Error('Failed to fetch all customers.');
}
}
export async function fetchFilteredCustomers(query: string) {
try {
const customers = await sql<CustomersTableType[]>`
SELECT
customers.id,
customers.name,
customers.email,
customers.image_url,
COUNT(invoices.id) AS total_invoices,
SUM(CASE WHEN invoices.status = 'pending' THEN invoices.amount ELSE 0 END) AS total_pending,
SUM(CASE WHEN invoices.status = 'paid' THEN invoices.amount ELSE 0 END) AS total_paid
FROM customers
LEFT JOIN invoices ON customers.id = invoices.customer_id
WHERE
customers.name ILIKE ${'%' + query + '%'} OR
customers.email ILIKE ${'%' + query + '%'}
GROUP BY customers.id, customers.name, customers.email, customers.image_url
ORDER BY customers.name ASC
`;
const formattedCustomers = customers.map((customer) => ({
...customer,
total_pending: formatCurrency(customer.total_pending),
total_paid: formatCurrency(customer.total_paid),
}));
return formattedCustomers;
} catch (err) {
console.error('Database Error:', err);
throw new Error('Failed to fetch customer table.');
}
}
`
Usingvercel dev
solved the issue for me
import postgres from 'postgres'; import { CustomerField, CustomersTableType, InvoiceForm, InvoicesTable, LatestInvoiceRaw, Revenue, } from './definitions'; import { formatCurrency } from './utils'; // Initialize the postgres client const sql = postgres(`${process.env.POSTGRES_URL}`); export async function fetchRevenue() { try { console.log('Fetching revenue data...'); const data = await sql<Revenue[]>`SELECT * FROM revenue`; console.log('Data fetch completed.'); return data; } catch (error) { console.error('Database Error:', error); throw new Error('Failed to fetch revenue data.'); } } export async function fetchLatestInvoices() { try { const data = await sql<LatestInvoiceRaw[]>` SELECT invoices.amount, customers.name, customers.image_url, customers.email, invoices.id FROM invoices JOIN customers ON invoices.customer_id = customers.id ORDER BY invoices.date DESC LIMIT 5`; const latestInvoices = data.map((invoice) => ({ ...invoice, amount: formatCurrency(invoice.amount), })); return latestInvoices; } catch (error) { console.error('Database Error:', error); throw new Error('Failed to fetch the latest invoices.'); } } export async function fetchCardData() { try { const invoiceCountPromise = sql`SELECT COUNT(*) FROM invoices`; const customerCountPromise = sql`SELECT COUNT(*) FROM customers`; const invoiceStatusPromise = sql` SELECT SUM(CASE WHEN status = 'paid' THEN amount ELSE 0 END) AS "paid", SUM(CASE WHEN status = 'pending' THEN amount ELSE 0 END) AS "pending" FROM invoices`; const [invoiceCountResult, customerCountResult, invoiceStatusResult] = await Promise.all([ invoiceCountPromise, customerCountPromise, invoiceStatusPromise, ]); const numberOfInvoices = Number(invoiceCountResult[0].count ?? '0'); const numberOfCustomers = Number(customerCountResult[0].count ?? '0'); const totalPaidInvoices = formatCurrency(invoiceStatusResult[0].paid ?? '0'); const totalPendingInvoices = formatCurrency(invoiceStatusResult[0].pending ?? '0'); return { numberOfCustomers, numberOfInvoices, totalPaidInvoices, totalPendingInvoices, }; } catch (error) { console.error('Database Error:', error); throw new Error('Failed to fetch card data.'); } } const ITEMS_PER_PAGE = 6; export async function fetchFilteredInvoices(query: string, currentPage: number) { const offset = (currentPage - 1) * ITEMS_PER_PAGE; try { const invoices = await sql<InvoicesTable[]>` SELECT invoices.id, invoices.amount, invoices.date, invoices.status, customers.name, customers.email, customers.image_url FROM invoices JOIN customers ON invoices.customer_id = customers.id WHERE customers.name ILIKE ${'%' + query + '%'} OR customers.email ILIKE ${'%' + query + '%'} OR invoices.amount::text ILIKE ${'%' + query + '%'} OR invoices.date::text ILIKE ${'%' + query + '%'} OR invoices.status ILIKE ${'%' + query + '%'} ORDER BY invoices.date DESC LIMIT ${ITEMS_PER_PAGE} OFFSET ${offset} `; return invoices; } catch (error) { console.error('Database Error:', error); throw new Error('Failed to fetch invoices.'); } } export async function fetchInvoicesPages(query: string) { try { const count = await sql` SELECT COUNT(*) FROM invoices JOIN customers ON invoices.customer_id = customers.id WHERE customers.name ILIKE ${'%' + query + '%'} OR customers.email ILIKE ${'%' + query + '%'} OR invoices.amount::text ILIKE ${'%' + query + '%'} OR invoices.date::text ILIKE ${'%' + query + '%'} OR invoices.status ILIKE ${'%' + query + '%'} `; const totalPages = Math.ceil(Number(count[0].count) / ITEMS_PER_PAGE); return totalPages; } catch (error) { console.error('Database Error:', error); throw new Error('Failed to fetch total number of invoices.'); } } export async function fetchInvoiceById(id: string) { try { const data = await sql<InvoiceForm[]>` SELECT invoices.id, invoices.customer_id, invoices.amount, invoices.status FROM invoices WHERE invoices.id = ${id}; `; const invoice = data.map((invoice) => ({ ...invoice, amount: invoice.amount / 100, // Convert amount from cents to dollars })); return invoice[0]; } catch (error) { console.error('Database Error:', error); throw new Error('Failed to fetch invoice.'); } } export async function fetchCustomers() { try { const customers = await sql<CustomerField[]>` SELECT id, name FROM customers ORDER BY name ASC `; return customers; } catch (err) { console.error('Database Error:', err); throw new Error('Failed to fetch all customers.'); } } export async function fetchFilteredCustomers(query: string) { try { const customers = await sql<CustomersTableType[]>` SELECT customers.id, customers.name, customers.email, customers.image_url, COUNT(invoices.id) AS total_invoices, SUM(CASE WHEN invoices.status = 'pending' THEN invoices.amount ELSE 0 END) AS total_pending, SUM(CASE WHEN invoices.status = 'paid' THEN invoices.amount ELSE 0 END) AS total_paid FROM customers LEFT JOIN invoices ON customers.id = invoices.customer_id WHERE customers.name ILIKE ${'%' + query + '%'} OR customers.email ILIKE ${'%' + query + '%'} GROUP BY customers.id, customers.name, customers.email, customers.image_url ORDER BY customers.name ASC `; const formattedCustomers = customers.map((customer) => ({ ...customer, total_pending: formatCurrency(customer.total_pending), total_paid: formatCurrency(customer.total_paid), })); return formattedCustomers; } catch (err) { console.error('Database Error:', err); throw new Error('Failed to fetch customer table.'); } }
I update app/lib/data.ts as mentioned above, also with pnpm install postgres
. Everything works here, seems like there exists problems using '@vercel/postgres'
Solution:
In /app/lib/data.ts
, I added client.sql
instead of sql
and that fixed it for me 🤷
- import { sql } from "@vercel/postgres";
+ import { db } from "@vercel/postgres";
+ const client = await db.connect();
...
- const data = await sql<Revenue>`SELECT * FROM revenue`;
+ const data = await client.sql<Revenue>`SELECT * FROM revenue`;
Make sure you do client.release();
This is the interface:
This is a project console error: Database Error: NeonDbError: Error connecting to database: fetch failed at execute (webpack-internal:///(rsc)/./node_modules/@neondatabase/serverless/index.mjs:1547:49) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async fetchRevenue (webpack-internal:///(rsc)/./app/lib/data.ts:25:22) at async Page (webpack-internal:///(rsc)/./app/dashboard/page.tsx:15:21) { code: null, sourceError: TypeError: fetch failed at node:internal/deps/undici/undici:12344:11 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async globalThis.fetch (webpack-internal:///(rsc)/./node_modules/next/dist/server/lib/patch-fetch.js:216:16) at async execute (webpack-internal:///(rsc)/./node_modules/@neondatabase/serverless/index.mjs:1546:43) at async fetchRevenue (webpack-internal:///(rsc)/./app/lib/data.ts:25:22) at async Page (webpack-internal:///(rsc)/./app/dashboard/page.tsx:15:21) { cause: AggregateError at internalConnectMultiple (node:net:1114:18) at internalConnectMultiple (node:net:1177:5) at Timeout.internalConnectMultipleTimeout (node:net:1687:3) at listOnTimeout (node:internal/timers:575:11) at process.processTimers (node:internal/timers:514:7) { code: 'ETIMEDOUT',
} } ⨯ app/lib/data.ts (31:10) @ fetchRevenue ⨯ Error: Failed to fetch revenue data. at fetchRevenue (./app/lib/data.ts:30:15) at async Page (./app/dashboard/page.tsx:15:21) 29 | } catch (error) { 30 | console.error('Database Error:', error);