#Problem fetching database within the app

25 messages · Page 1 of 1 (latest)

tight slateBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

lunar nest
#

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

honest rain
#

But this is your local env right? Did you paste your env vars into your Vercel env vars?

lunar nest
#

Vercel setups the env variables

#

They doesn't seem to be reachable inside the app

#

My last hope was to use ts import { config } from 'dotenv'; config();

#

but didn't work

near wind
lunar nest
#

To my .env file? Yes

#

Now it's called .env, cause .env.development and .env.development.local weren't working anyways

honest rain
#

Your .env files aren't uploaded to Vercel on their own, how are you supplying it to Vercel? Usually people copy paste their env vars into the Vercel console

near wind
lunar nest
#

Are them on vercel?

#

I'm pretty sure those are the uploaded env variables

#

On my proyect's settings tab

near wind
#

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

lunar nest
#

That was the problem. Enviroment file is supplied but Its values cannot be read from the app

#

"undefined" for all variables

honest rain
lunar nest
near wind
#

Does it work on vercel?

lunar nest
#

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

lunar nest
#

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.