Hello everyone
For a project, I’m using Netlify functions to access an environment variable stored in Netlify and making some fetch calles. This is working just fine on my local netlify dev
server, but breaks on the deployed website with the following error:
I bought a custom domain for this website today (also through Netlify) so I’m suspecting that that’s breaking it, especially since the whole thing is working just fine in the development environment.
Site: https://springrainhub.com/
Github Repo: https://github.com/AlonCohen96/spring-rain
This is how I am calling the netlify function in my frontend in App.js:
async function getStockSummary(ticker){
setLoading(true)
let data
try {
let response = await fetch(`.netlify/functions/getApiKey?ticker=${ticker}`)
data = await response.json()
} catch (error){
setLoading(false)
alert('The requested ticker is currently unavailable in our database, please try again in a moment and check for spelling mistakes.')
return
}
This is my Backend Code inside netlify/functions folder:
const options = {
method: 'GET',
headers: {
'X-RapidAPI-Key': process.env.API_KEY,
'X-RapidAPI-Host': 'yh-finance.p.rapidapi.com'
}
}
exports.handler = async function (event, context) {
const { ticker } = event.queryStringParameters;
let response = await fetch(`https://yh-finance.p.rapidapi.com/stock/v2/get-summary?symbol=${ticker}®ion=US`, options)
let stockSummary = await response.json();
let response2 = await fetch(`https://yh-finance.p.rapidapi.com/stock/v2/get-chart?interval=1d&symbol=${ticker}&range=1y®ion=US`, options)
let chartData1y = await response2.json();
let response3 = await fetch(`https://yh-finance.p.rapidapi.com/stock/v2/get-chart?interval=1d&symbol=${ticker}&range=max®ion=US`, options)
let chartDataMax = await response3.json();
let response4 = await fetch(`https://yh-finance.p.rapidapi.com/stock/v2/get-chart?interval=15m&symbol=${ticker}&range=1mo®ion=US`, options)
let chartData1m = await response4.json();
return {
statusCode: 200,
body: JSON.stringify({
stockSummary: stockSummary,
chartData1y: chartData1y,
chartDataMax: chartDataMax,
chartData1m: chartData1m
})
};
}
Would greatly appreciate any help, as I am quite lost on this one.
Best,
Alon