Next.js Discord

Discord Forum

Problem fetching database within the app

Answered
Jersey Wooly posted this in #help-forum
Open in Discord
Original message was deleted.
Answered by Jersey Wooly
I can't access server-side functions from the client-side components, in order to prevent data from leaking. I was trying to not use fetch(url) but there is no other proper way.
View full answer

19 Replies

Jersey Wooly
This is get-all-clients
import { sql } from '@vercel/postgres';
import { NextResponse } from 'next/server';

export async function GET() {
    try {
        const result = await sql`SELECT * FROM clients;`
        return NextResponse.json({ rows: result.rows }, { status: 200 });
    } catch (err) {
        console.error(err);
        return NextResponse.json({ err }, { status: 500 });
    }
}
Next is also correctly using env variables. Forgot to mention that .env is on the root folder
Jersey Wooly
Vercel setups the env variables
They doesn't seem to be reachable inside the app
My last hope was to use
import { config } from 'dotenv';
config();
but didn't work
@Jersey Wooly Vercel setups the env variables
Savannah
What do you mean, it only supplies the variables on their side. Did you copy the variables to your local .env file, since you are testing it on localhost
Jersey Wooly
To my .env file? Yes
Now it's called .env, cause .env.development and .env.development.local weren't working anyways
I'm pretty sure those are the uploaded env variables
On my proyect's settings tab
Savannah
That's right, you probally linked the database to the project, but that doesn't have to do anything with testing on localhost. Did you check if POSTGRES_URL is in your local .env file or you missed to copy it. Normally you dont need anything custom to load .env file
Jersey Wooly
That was the problem. Enviroment file is supplied but Its values cannot be read from the app
"undefined" for all variables
@Jersey Wooly My last hope was to use ts import { config } from 'dotenv'; config();
Jersey Wooly
That's why i've tried this
Savannah
Does it work on vercel?
Jersey Wooly
Where do I test it on vercel? Env variables work when fetching api's urls from the browser
doesn't make any sense to me
Jersey Wooly
I can't access server-side functions from the client-side components, in order to prevent data from leaking. I was trying to not use fetch(url) but there is no other proper way.
Answer