Getting Error in "Use the .env File" Exercise

Tell us what’s happening:
I am getting this error:
The response of the endpoint /json should change according to the environment variable MESSAGE_STYLE`

Your code so far
Code in .env file:
MESSAGE_STYLE = uppercase;

Code in myApp.js file:

app.get("/json", function (req, res) {
  if (process.env.MESSAGE_STYLE === 'uppercase') {
    res.json({
      "message": "Hello json"
    });
  }
    res.json({
      "message": "Hello json"
    });  
});

Link to Project Files
Repl.it

Your browser information:

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

Challenge: Use the .env File

Link to the challenge:

Welcome, ehsanulhaque.

One thing to mention: A few users are having issues with the incorrect .env variable:

  • To debug: add this line somewhere in your code:
console.log(process.env.MESSAGE_STYLE)
  • If you get undefined in the logs, and you have defined it, you need to add the following package: dotenv, then call it at the top of your script:
    require('dotenv').config();

As for your code, the instructions are:

…if process.env.MESSAGE_STYLE equals uppercase . The response object should become {"message": "HELLO JSON"} .

Can you see the problem?

1 Like

Hi @Sky020,

It’s nice to meet you.

As suggested, I have put console.log(process.env.MESSAGE_STYLE); at the top of myApp.js file. As a result, I am seeing uppercase in the console, which seems to be fine. It looks like I have defined MESSAGE_STYLE at .env file perfectly.

Link to Project Files:
Repl.it

However, I am still getting the same error while trying to submit my solution on FreeCodeCamp:
The response of the endpoint /json should change according to the environment variable MESSAGE_STYLE`

How can I fix the issue?

Another thing. I have changed "message": "Hello json" to "message": "HELLO JSON", as stated in the challenge.

Hi @Sky020,

I have found the solution. In myApp.js file, the codes should be like this:

app.get("/json", function (req, res) {
  if (process.env.MESSAGE_STYLE === 'uppercase') {
    res.json({
      "message": "HELLO JSON"
    });
  }
    res.json({
      "message": "Hello json"
    });  
});

Link to My Solution:
Repl.it

Now, the issue has been fixed.

1 Like

In the .env file, there should be no spaces around = and in your myApp.js file, if MESSAGE_STYLE is uppercase, the you want to send HELLO JSON else hello json