Environment variables challenge

Tell us what’s happening:
i’m getting the requested results but the test is not passing. i don’t know why

Your project link(s)

solution: https://replit.com/@federicopieruzz/boilerplate-express

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36

Challenge: Use the .env File

Link to the challenge:

Note: If you are using Replit, you cannot create a .env file. Instead, use the built-in SECRETS tab to add the variable.”

Did you do this? What did you type in there?

I already solved it. Turns out if you do the if check outside of the handler function the test fails for some reason even though you get the exact same result

You don’t get the exact same result though. If you check the environment variable in the global scope, then your app does not respond to changes to the environment variable.

Yes it did, i checked multiple times. I changed the environment variable’s value to “asd” (without quotations of course) and the message would be lowercase

I’m sorry, but no, it did not.

Placing the line of code where you check the value of the environment variable outside of the handler function means that the environment variable is checked only once, when the application first starts.

However, when you check the value of the environment variable inside the handler function, then you check the value of the environment variable every time the handler function is called.

This difference is important because the test suite sends a command to your application to change the environment variable while the application is running. If you do not check the value of the environment variable inside of your handler, then it is literally impossible for your handler to respond to this change.

Manually changing the environment variable involves restarting the application, which isn’t how the test suite works. It also isn’t how you would want a real world application to work. You shouldn’t need to restart the application to change this behavior.

2 Likes

Aahh yes, i understand now. I guess i completely missread that in the challenge description. Thank you for responding

No worries. It took me a while to figure out what was up the first time I saw someone have ttis issue since I’ve not done a lot of this sort of development.

Then why do i get undefined with this code

app.get('/json', (req, res) => {
  const mySecret = process.env['MESSAGE_STYLE'];

  console.log(mySecret)
  
})

Does this do it?

Mod Edit: SOLUTION REDACTED

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

1 Like

Hi JeremyLT, Ok thanks yes that makes a lot of sense so that the other people go through the same learning process and headaches that we had. Yes, totally. This was a difficult one for me to understand as well.