Basic Node and Express - Use the .env File

Tell us what’s happening:
Project is giving me the uppercase as I needed but cannot pass the test. It keeps saying that cannot find the environment variable.
I wrote
const mySecret = process.env.MESSAGE_STYLE
const mySecret = process.env[‘MESSAGE_STYLE’]
inside the route handler, but there’s no difference.

Your project link(s)


Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0

Challenge: Basic Node and Express - Use the .env File

Link to the challenge:

We need to see your code to help, but your link does not work.

Here is the fixed link. boilerplate-express (1) - Replit

    if (process.env.MESSAGE_STYLE === 'uppercase') {
     var u_=JSON.parse(JSON.stringify(messageObject ));
     return res.json(u_);
  } else {
      return res.json(messageObject);

I don’t know what you are doing here, but I liked this better:

  // if(mySecret === "uppercase") {
  //     res.json({"message": "Hello json".toUpperCase()});
  // } else {
  //     res.json({"message": "Hello json"});
  // }

Thank’s for the quick response.
I made another fix based on your anwer but test keeps failing.

What link are you submitting?

You can do some debugging yourself to watch what happens when the tests run:

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

  if (mySecret === "uppercase") {
    console.log("responding 'HELLO JSON'");
    res.json({"message": "Hello json".toUpperCase()});
  } else {
    console.log("responding 'Hello json'");
    res.json({"message": "Hello json"});

I sent this link:
and it works like a charm.

This is the result:

This means that you are never providing the lowercase result?

The lowercase is in the second if condition, the else, so it never reach it, because the first condition is true: there’s an uppercase text into the environment variable.
I test it in a Gitpod account and it works fine. I think is a replit issue because of the way it handles the environment variables through that Tool/Secrets feature.

So you should check if you set up the secret per the instructions

Dear God, you’re right. I sent it without the “/json” and now the tests are all good.
Thanks a lot, both of you.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.