Reputation: 3
I am programming a node.js app and i need to deploy it on heroku. In it, i am getting 3 APIs, one of them requires an API key from Rapidapi. In my local directory everything works fine. But as soon as I upload my app to heroku the one that requires the API key doesn't work anymore.
Here is my code:
//where i am accessing my api
fetch(`/geodb?namePrefix=${city}`).then(response => { //fetching country data to city name
response.json().then(response => {
response.json = response;
var citylenght = Object.keys(response.data).length;
}
//and here is the actual fetch request:
app.get('/geodb', async (request, response) => {
let cityname = request.query.namePrefix; //getting parameter for the url
let api_response = await fetch(`https://wft-geo-db.p.rapidapi.com/v1/geo/cities?namePrefix=${cityname}`, {
"method": "GET",
"headers": {
"x-rapidapi-host": "wft-geo-db.p.rapidapi.com",
"x-rapidapi-key": process.env.API_KEY //loading api key from .env file
}
});
let json = await api_response.json();
response.json(json);
});
On my localhost everything is working fine, but when I am accessing my app on heroku the browser console says the following:
> Uncaught (in promise) TypeError: Cannot convert undefined or null to
> object
> at Function.keys (<anonymous>)
> at api.js:13 (anonymous) @ api.js:13 Promise.then (async) (anonymous) @ api.js:11 Promise.then (async) lookup @ api.js:9
> receiveData @ listeners.js:7 onclick @ (index):43 (anonymous) @
> (index):187
That tells me that the app is unable to access the api data and thats why the answer is undefined. The heroku log doesn't show any error either.
By the way: When I am just using the 2 other apis that don't need any api key, and where i didnt define any headers, then everything works fine.
I would be very happy if you could help me because I need to get it done very soon.
Thanks!
Upvotes: 0
Views: 1801
Reputation: 1986
On Heroku, you set your Environment Variables in the application config, not by uploading .env
. You can either use the web interface or the heroku config
command if you use the CLI tool.
Make the key "API_KEY" and the value whatever it should be from your .env
file (and probably don't deploy that file, they are usually kept local and not included in source control or deployed to the hosting platform. This part is just advice though!)
Upvotes: 1