Even with the environment variables set, it fallbacks to default
Unanswered
Australian Freshwater Crocodile posted this in #help-forum
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(
);
const data = await apiResponse.json();
return new Response(JSON.stringify({ data: data }));
} catch (error) {
console.log(
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?
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?