I am wanting to console.log data from this API when I hit the search button. I am just starting out with React and so I’m not entirely sure how to this.
@snowmonkey I assume render is in an index.js or something.
@fkhan698 what are you getting back? There doesn’t seem to be anything wrong with what you have, so you might have to check the API docs if it isn’t giving you what you want.
I got it to work again after I took out the .env and just left the API keys in the App.js. But whenever I try to use the env I get the same errors. And the .env file is in the root
Not sure what the issue is then. Did you restart the app after adding the .env file? Maybe also double check that you are using the long number for the API_KEY and the short one for the API_ID. Other than that I can’t really think of anything.
You should be able to use dotenv to load env files from the root of your directory, so outside the src folder. Then just import dotenv into the component and set the API key to REACT_APP_API_KEY={whatever your API key is} inside of the env file.
Set the path of the env file to dotenv.config({ path: "./config.env" }); inside the component then you can pull your API key using process.env.REACT_APP_API_KEY.
so your url for the response should look like this:
I think that might be what I am missing. I forgot to import the .env file. So do I put the dotenv.config({ path: "./config.env" }); line at the top of my App.js which is the code I had above?
You don’t need to do anything. As long as the .env file is in the root and you are using create-react-app.
As said, I tested this locally using the code you posted with my own API id/key and it works just fine. You can make a private Github repo and PM the link if you want.
@fkhan698 If you are still looking for a solution, perhaps this lesson on the venerable Scrimba platform could help steer you in the right direction…? https://scrimba.com/p/p7P5Hd/c79Jask
Oddly, this was all working yesterday - even have screenshots of the functional program. Now, I’m getting an error.
When I hard code the app key into App.js, it works. When I switch it back to the variable referring to .env, it only loads the search bar with no recipes.