Next.js Discord

Discord Forum

Even with the environment variables set, it fallbacks to default

Unanswered
Australian Freshwater Crocodile posted this in #help-forum
Open in Discord
Australian Freshwater CrocodileOP
This is my code to a route.js in nextjs app

export async function GET(req, res) {
const { searchParams } = new URL(req.url);
const symbol = searchParams.get("symbol") "IBM";
// const symbol = "IBM";
const apiKey = process.env.API_KEY
"demo";
console.log("symbol", symbol, "apiKey", apiKey);
try {
const apiResponse = await fetch(
https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=${symbol}&outputsize=full&apikey=${apiKey}
);
const data = await apiResponse.json();
return new Response(JSON.stringify({ data: data }));
} catch (error) {
console.log(Error: ${error.message});
return new Response.json({ error: error.message });
}
}

console.log() of apiKey gives demo, if i remove the fallback, it gives undefined but also uses the intended value provided by the environment. Why this behaviour?

0 Replies