Use .env file in Replit

Tell us what’s happening:
After going through multiple other forum posts, YouTube videos, and freeCodeCamp’s “Get a Hint” button, I cannot figure out what is wrong with my code when trying to get through the .env portion.

I have created the “Secret” tab .env key and value, so I am unsure if I went wrong there. It is MESSAGE_STYLE=uppercase.

I have starred at this for so long I can’t think of anything else to try. Any help would be appreciated.

Your project link(s)

solution: https://replit.com/@rainbowkitty876/boilerplate-express-1

Your browser information:

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

Challenge: Use the .env File

Link to the challenge:

2 Likes

on the left side of replit app. there is a tab called ‘secret’, put your .env variable there. and access that variable in your code using process.env.YOUR_VAR
and dont forget to require('dotenv').config() at the beginning of your code

1 Like

I have the MESSAGE_STYLE=uppercase in the secrets tab already with the key being MESSAGE_STYLE. Within the app.get() I have the const mySecret = process.env[‘MESSAGE_STYLE’] above the ‘if’ statement as shown below.

app.get("/json", function(req, res) {
const mySecret = process.env[‘MESSAGE_STYLE’];
if (mySecret === “uppercase”){
res.json({ “message”: “Hello json”.toUpperCase() });
} else {
res.json({ “message”: “Hello json” });
};
});

And still it shows up with the error " The response of the endpoint “/json” should change according to the environment variable “MESSAGE_STYLE”

res.json({"message":"Hello json"}.toUpperCase());

what are you making uppercase? this is what I see in your replit, not

like you wrote in your post

1 Like

Ouh sorry I get it wrong…
I solve the problem by just returning {message: "HELLO JSON"} when its true
not using toUpperCase().

I had changed it to mess around and see if anything else would work. I stepped away before I could change it back but I originally had it the way I did in my post.

your issue may be here

can you show a screenshot of the secrets tab?
generally, never share your environment variables, but in this case it is not a password or API token, so it’s fine

It has been like this since I started this section, without changing it. I wouldn’t doubt if something was wrong here but I wouldn’t know what.

in the form value should only ‘uppercase’ and key ‘MESSAGE_STYLE’

you set the value to be “MESSAGE_STYLE=uppercase”, the value should be only “uppercase”

After changing it, I still get the same error when checking my answer and this is what it all looks like currently.

1 Like

you pressed Save, right?

Yes, and I double checked it as well after it failed the first time.

have you saved the project?
I still see

  res.json({"message":"Hello json"}.toUpperCase());

I changed it when I got back to my desk. The updated code would be in that screenshot I just sent so now it reads:

app.get("/json", function(req,res) {
const mySecret = process.env[‘MESSAGE_STYLE’];
if (mySecret === “uppercase”) {
res.json({“message”: “HELLO JSON”});
} else {
res.json({“message”: “Hello json”});
};
});

1 Like

yes, but the live app doesn’t necessarily have that code - have you pressed Run recently? have you saved? I still don’t see the updated code in the replit, so the app you submit doesn’t have the new code

I have ran it and saved it a few times since I’ve changed code. I just closed and reopened it through the link up top and it was updated as well

still nothing on my side :confused:

if you go to the live app link, what do you see?
https://boilerplate-express-1.rainbowkitty876.repl.co

and the json endpoint?
https://boilerplate-express-1.rainbowkitty876.repl.co/json

though submitting this the tests pass

I don’t know what changed from that last screenshot I sent, but after checking multiple times it finally said it was correct.

The live app link shows “Not Found” and the json endpoint shows "{“message”: “HELLO JSON”}

I do think the biggest thing that was keeping me from getting this correct was the secret key thing. Thank you!