Troubleshooting Connection String Error in Page Context for SQL Query Execution
Answered
Giant panda posted this in #help-forum
Original message was deleted.
143 Replies
Giant panda
I have a SQL query that works perfectly in routes, but encounters an issue when used on a page. The query is as follows:
This is executed in an environment defined by .env.development.local. However, when I try to use this SQL query in a page, I encounter an error stating: 'Error fetching data: VercelPostgresError - 'missing_connection_string': You did not supply a 'connectionString' and no 'POSTGRES_URL' env var was found.
const sqlResponse = await sql`
SELECT *
FROM Profiles
WHERE ProfileID = ${params.profile};
`;This is executed in an environment defined by .env.development.local. However, when I try to use this SQL query in a page, I encounter an error stating: 'Error fetching data: VercelPostgresError - 'missing_connection_string': You did not supply a 'connectionString' and no 'POSTGRES_URL' env var was found.
it works pefectly fine when using routes
but somehow not in pages
@Giant panda but somehow not in pages
try
console.log(process.env.POSTGRES_URL) on the pageGiant panda
"undefined"
@Giant panda "undefined"
how about
console.log(process.env.POSTGRES_URL) on the routeGiant panda
i only have these
ok so i copied this and pasted it
still its "undefined"
@Giant panda still its "undefined"
hmm how does it work if its undefined
Giant panda
no idea it works in routes tho
just not in pages
are you on dev mode?
Giant panda
yep
@Giant panda Click to see attachment
where do you import
sql from@Ray where do you import `sql` from
Giant panda
import {sql} from "@vercel/postgres";
@Ray do you see `.env.development.local` being loaded when the dev server started?
try rename
.env.development.local to .env@Ray try rename `.env.development.local` to `.env`
Giant panda
still same
@Giant panda still same
could you show a screenshot of your folder structure
@Ray could you show a screenshot of your folder structure
Giant panda
sorry for the delay
@Giant panda sorry for the delay
you had 2 env file?
Giant panda
yea
removing it changes nothing tho
@Giant panda removing it changes nothing tho
could you show a screenshot of the console after you start the dev server
Giant panda
(after removing the other .env)
@Giant panda (after removing the other .env)
try console.log(process.env) on the page
Giant panda
it did work
@Giant panda Click to see attachment
you had
.env.development.local and .env before?Giant panda
yea
and which on did you put
POSTGRES_URL?Giant panda
.env
well before .env.development..
but then u said .env
try moving it to
.env.development.localif NODE_ENV is development and you define a variable in both .env.development.local and .env, the value in .env.development.local will be used.
Giant panda
same error
@Giant panda (after removing the other .env)
which one is loaded?
restart the server
Giant panda
@Giant panda Click to see attachment
you only have
.env.development.local now?Giant panda
yes
same error still
Giant panda
same error
.env doesn't work too now?
Giant panda
nope
this is so weord
restart the server
Giant panda
i did
did it load from .env now
Giant panda
yea but same error
is it because of "use client"
@Giant panda is it because of "use client"
oh you using it on client component?
Giant panda
yea
you can't use it in client component
Giant panda
hm
how i should use it then? without using routes
@Giant panda how i should use it then? without using routes
use it in server component
without
'use client'Giant panda
like this?
@Giant panda Click to see attachment
what are you gonna do with this function?
yes if you gonna use this with server action
Answer
@Ray what are you gonna do with this function?
Giant panda
so im doing a free portfolio service i just want to get data from the profile
and display it on the site
without using any api
@Giant panda so im doing a free portfolio service i just want to get data from the profile
you don't need 'use server' for that
everything are server by default
unless you mark it to client
@Ray you don't need 'use server' for that
Giant panda
didnt work
when i dont put "use server"
same error comes back
@Giant panda when i dont put "use server"
you should remove
'use client'Giant panda
well it works they way im using it
so all good
works perfectly fine
Giant panda
yea
i just want the data to be leaked
ok cool
Giant panda
like the proflile contains secretKey
thats what shouldnt be public
no, your key not gonna leak
but seo engine will not be able to crawl your site
Giant panda
thats totally fine
thanks bro
ok no prob
Giant panda
@Ray
It shows in the network still
@Giant panda Click to see attachment
the query result?
@Ray the query result?
Giant panda
yea
@Giant panda yea
it is expected
Giant panda
not possible to hide?
what you gonna do with this data
fetch on server
Giant panda
display it
nothing else
why would you wanna hide it if you gonna display it?
it will show on the dom anyway?
Giant panda
bcs some of the columns shouldnt not gonna be displayed
only some parts
@Giant panda bcs some of the columns shouldnt not gonna be displayed
then don't use
select *only select the column you need
Giant panda
not possible any other way?
fetch on server
Giant panda
wdym
no other way
don't fetch on client
@Ray on client side
as I said earily
Giant panda
well i fetch it on "use server" and just pass the data to the client
you are fetching on client side
with server action
Giant panda
how i should do it? can you give me an example

@Giant panda how i should do it? can you give me an example
fetch in server component
remove 'use client'
Giant panda
ahh this is so bad
@Giant panda ahh this is so bad
what is it bad?
Giant panda
i dont get it how i should split the files with "use xxx"
its weird asf
you don't even need
'use client' useState setData useEffect if you fetch on server@Giant panda i dont get it how i should split the files with "use xxx"
actually, you don't even need
'use server' thereGiant panda
i use useEffect to add views to the user
much simple
Giant panda
and loading screen
import { ProfileLayout } from "@/components/Layout";
import { useEffect, useState } from "react";
import { Button, Spinner } from "@nextui-org/react";
import Link from "next/link";
import {getUserData} from "@/app/[profile]/getprofile";
export default function Page({ params }) {
const [isLoading, setIsLoading] = useState(true);
const [data, setData] = useState(null);
useEffect(() => {
const fetchData = async () => {
try {
const response = await getUserData(params.profile)
setData(JSON.stringify(response[0]));
} catch (error) {
console.error('Error fetching data:', error);
} finally {
setIsLoading(false);
}
};
fetchData();
}, [params.profile]);
useEffect(() => {
const updateVisitedProfiles = () => {
if (data) {
const currentProfile = params.profile;
const visitedProfiles = JSON.parse(localStorage.getItem('visitedProfiles') || '{}');
if (!visitedProfiles[currentProfile]) {
fetch(`api/increaseViews?profileID=${currentProfile}`);
localStorage.setItem('visitedProfiles', JSON.stringify({
...visitedProfiles,
[currentProfile]: true
}));
}
}
};
updateVisitedProfiles();
}, [data, params.profile]);
if (isLoading) {
return (
<div className="bg-[#0A0B0C] flex h-screen w-full items-center justify-center">
<Spinner size="lg" className="scale-150" />
</div>
);
}
if (!data) {
return (
<div className="flex flex-col text-center items-center justify-center min-h-screen bg-gradient-to-br from-primary p-8">
<p className="text-4xl text-white mb-8 font-bold">Page not found</p>
<Button as={Link} href="/" color="primary" variant="faded">Go to Homepage</Button>
</div>
);
}
return <ProfileLayout Profile={data} />;
}