next-learn icon indicating copy to clipboard operation
next-learn copied to clipboard

Chapter 7 - fetching data from the DB Error: Failed to fetch revenue data.

Open nikolabaci98 opened this issue 1 year ago • 2 comments

Solved by replacing:

import { sql } from '@vercel/postgres'; 
.
.
.
await sql< Revenue >

with

import { db } from '@vercel/postgres';
const client = await db.connect();
.
.
.
await client.sql< Revenue >

nikolabaci98 avatar Dec 02 '24 22:12 nikolabaci98

Solved this by replacing:

import { sql } from '@vercel/postgres'; await sql< Revenue >

with

import { db } from '@vercel/postgres'; const client = await db.connect(); await client.sql< Revenue >

This solution doesn't work for me.

My solution is replacing:

const data = await sql<Revenue>`SELECT * FROM revenue`;

with

const data = await sql.query<Revenue>('SELECT * FROM revenue');

liuxiaohui avatar Dec 03 '24 04:12 liuxiaohui

Same problem, both solutions work for me.

mooksz avatar Dec 03 '24 11:12 mooksz

Do these solutions work for neon and supabase or is there another?

CogSciOliver avatar Dec 07 '24 09:12 CogSciOliver

You can switch to postgres which is an agnostic library which works with any Postgres database! We will likely update that here.

leerob avatar Dec 07 '24 14:12 leerob

Do these solutions work for neon and supabase or is there another?

liuxiaohui's solution worked for me, using supabase

aviel-fahl avatar Dec 18 '24 16:12 aviel-fahl

Solved this by replacing: import { sql } from '@vercel/postgres'; await sql< Revenue > with import { db } from '@vercel/postgres'; const client = await db.connect(); await client.sql< Revenue >

This solution doesn't work for me.

My solution is replacing:

const data = await sql<Revenue>`SELECT * FROM revenue`;

with

const data = await sql.query<Revenue>('SELECT * FROM revenue');

perfect!

studiobythepine avatar Dec 30 '24 11:12 studiobythepine

This worked just perfect https://github.com/vercel/next-learn/issues/943#issuecomment-2565387462

FaustoSav avatar Jan 11 '25 21:01 FaustoSav

Glad to hear these solutions worked for you all!

leerob avatar Jan 15 '25 03:01 leerob

TL;DR use 'NEON' DB instead of supabase or soemthing.

My case was solved without changed the code.

I installed "supabase" and it was the cause of problem. I deleted the supaabase db and installed neon and it wroks. Try this.

deepseek-r1 solved this for me :)

### **Diagnosis & Solution Summary**  
The current `@vercel/[email protected]` is designed for integration with **Neon Serverless PostgreSQL**.  
However, your error message indicates the use of a **Supabase host (`api.pooler.supabase.com`)**, meaning the **connection string is pointing to Supabase instead of Neon**, which is the core issue.

---

### 1. **Root Cause**  
- **Mismatch Between Library and Connection Target**  
  - `@vercel/[email protected]` is built for **Neon**, but your environment variable (`DATABASE_URL`) is using a **Supabase connection string**.  
  - Result: The library attempts to connect to Supabase using the Neon driver (`@neondatabase/serverless`) → DNS error occurs.  

---

### 2. **Solution**  
#### (1) **Full Migration to Neon** [Recommended]  
- **Use Vercel Postgres + Neon**  
  1. Create a **Neon Database** via the Vercel Dashboard and connect it.  
  2. Apply the auto-generated **Neon connection string** to your `.env` file:  
     ```env  
     DATABASE_URL="postgres://user:pass@neon-host:5432/db?sslmode=require"  
     ```  
  3. Continue using the `sql` helper in your code:  
     ```ts  
     import { sql } from '@vercel/postgres';  
     await sql`...`;  
     ```  

icedac avatar Jan 26 '25 10:01 icedac