env variable is undefined in server side
Answered
Siberian Blue Robin posted this in #help-forum
Siberian Blue RobinOP
Hi i need help in getServerSideProps :
So env variable logs but i can't use it in fetch method,
what kind of problem this seems to be ?
export async function getServerSideProps({ locale, query }) {
console.log(process.env.URL_API_GATEWAY); // this logs well https://...
try {
const response = await fetch(`${process.env.URL_API_GATEWAY}/article/v1/articles/${query.id}`); this returns undefined
if (!response.ok) {
throw new Error("Failed to fetch.");
}
const item = await response.json();
return {
props: {
item,
...(await serverSideTranslations(locale, ["common", "supply", "api"])),
},
};
} catch (error) {
console.error("Error fetching data:", error);
return {
props: {
item: null,
...(await serverSideTranslations(locale, ["common", "supply", "api"])),
},
};
}
}So env variable logs but i can't use it in fetch method,
what kind of problem this seems to be ?
Answered by bscdev#1145
You should define env variable in a below way as well as .env file should be located in your project's root directory.
URL_API_GATEWAY=your key
thats it!
URL_API_GATEWAY=your key
thats it!
3 Replies
You should define env variable in a below way as well as .env file should be located in your project's root directory.
URL_API_GATEWAY=your key
thats it!
URL_API_GATEWAY=your key
thats it!
Answer
Siberian Blue RobinOP
SOLVED,
true it the / that i added twice in the url, thx
true it the / that i added twice in the url, thx