Use the .env File - are tests ok?

Short description

In “Use the .env File” challenge: first test says my code is ok while the second doesn’t.

Issue Description

Hello guys, I am working on “Use the .env File” challenge in the beta curriculum. I have managed to write my code with ES5 standards and when I test it on Glitch it works fine:

  • if I visit https://lead-racer.glitch.me/json with MESSAGE_STYLE=uppercase, the output will be {"message":"HELLO JSON"}

  • without any value for MESSAGE_STYLE, the output will be {"message":"Hello json"}

If I test my url https://lead-racer.glitch.me/ on freeCodeCamp page, the first test says my code is ok and takes me to the following challenge. However, if for any reason I test the url a second time, it says The response of "/json" does not change according to MESSAGE_STYLE

In my case, I commented out the working code to refactor it with ES6 standards and arrow functions. I made the usual tests from https://lead-racer.glitch.me/json and they were ok but when I tested on freeCodeCamp I got that error.

I thought I made some mistake with arrow functions (I don’t use them that much) so I brought back the previous code: surprisingly, the test on freeCodeCamp failed again :face_with_raised_eyebrow:

I found a way to pass the test, which is:

  • cut the code from Glitch IDE

  • refresh the Glitch IDE

  • paste the code again and wait for the Live label

  • refresh the challenge page on FCC

  • paste the Glitch URL

  • launch the test that will pass

  • avoid any additional test and go to the next challenge :joy:

However, I am not sure this is the best way to solve the problem. Any suggestions from other campers?

My Code with ES5

let messageObject = {"message": "Hello json"};

app.get("/json", jsonHandler);

function jsonHandler(req, res) {
  if (process.env.MESSAGE_STYLE == "uppercase") {
    messageObject.message = messageObject.message.toUpperCase()
  } 
  return res.json(messageObject);
  }

In .env I have:

MESSAGE_STYLE=uppercase

I just tried your code, it works fine. My code, on the other hand, doesn’t work… so I was wondering if the tests check for toUpperCase method specifically.

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

Then I tried a modification of your code as shown below. That didn’t work at all, which leads me to think that the testing unit might be bugged… Or having variable name as the key name confused the test unit somehow…

let message = {"message": "Hello json"};

app.get("/json", jsonHandler);

function jsonHandler(req, res) {
  if (process.env.MESSAGE_STYLE == "uppercase") {
    message.message = message.message.toUpperCase()
  } 
  return res.json(message);
}
1 Like

you probably already fixed it by now but if you haven’t try this:

"let messageObject = {“message”:“Hello json”};

app.get("/json", jsonHandler);

function jsonHandler(req, res){
if(process.env.MESSAGE_STYLE===‘uppercase’) {
messageObject.message = messageObject.message.toUpperCase()
}
return res.json(messageObject);
};"

It worked for me! :smiley:

1 Like

FYI - I had a problem that was causing my tests not to pass that I wanted to share here.

If your tests aren’t passing, check your if statement for == vs. ===

This code doesn’t pass:

if (process.env.MESSAGE_STYLE == ‘uppercase’) {
helloJSON.message = helloJSON.message.toUpperCase();
}

This code passes:

if (process.env.MESSAGE_STYLE === ‘uppercase’) {
helloJSON.message = helloJSON.message.toUpperCase();
}

1 Like

I think there is no need to use a return keyword, though adding it also pass the test.

You should define your function before app.get(PATH, FUNCTION)

let messageObject = {"message": "Hello json"};

function jsonHandler(req, res) {
  if (process.env.MESSAGE_STYLE == "uppercase") {
    messageObject.message = messageObject.message.toUpperCase()
  } 
  return res.json(messageObject);
}

app.get("/json", jsonHandler);
1 Like