`Use the .env file` code fails the test, there is no official solution either

Tell us what’s happening:
A .env file is created. The following is added to it:
MESSAGE_STYLE=uppercase.

The test fails with the code below.
https://repl.it/@controlunit/boilerplate-express#myApp.js

Your code so far

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

Your browser information:

Chromium

Challenge: Use the .env File

Link to the challenge:

I believe json should be lowercase if not toUpperCase.

Also, you are hard coding the message. If this were a real world scenario, if you wanted to change the message you would have to change it on two lines which could lead to errors and more time spent maintaining the code.

If you make the message a variable, and then wanted to change the message you would only have to change it on one line.

For example in semi-pseudo code:
let message = “Hello json”
if (my message should be uppercase) message = message.toUpperCase()
res.json({“message”: message})

The code has been changed according to your suggestion, however, the test still fails:

var lower = "hello json"
var upper = "hello json".toUpperCase

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

You have a couple of issues:

var upper = "hello json".toUpperCase

toUpperCase is a method (function). How do you call a functon?

The value for the message property should be the same that was used in the previous lesson, unless process.env.MESSAGE_STYLE is equal to “uppercase”. You are assigning the wrong value to lower here.

@Randell That solution is working. Very much appreciated, thanks!

Hi, sorry but I am still stuck (almost at the same spot).

Could you please have a look ?
https://repl.it/join/kfoycreu-matdomichard

const sentence = "hello json";

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


Similar to what I said in the previous post (see below).

You have a typo for the value of message when process.env.MESSAGE_STYLE is not equal to “uppercase”.

Just assign the correct default value to sentence and then use sentence as the message property’s value for the else instead of trying to hard-code it like you are doing now.

1 Like

Try using Glitch, I couldn’t solve this using Repl.It no matter what I’ve tried…

1 Like

Thank you so much ! I know that it’s the same explanation but I didn’t understand it the first time.
Thanks again, it finally passed.

You didn’t catch on to what I was suggesting and made two variables rather than one and changed the lowercase string. Which means you still have the same problem I was alluding to. I think @RandellDawson provided more insight on the problem to help understand. If you still have more questions I would be happy to help.

Yes, actually how to pass the test for the following, here: (MongoDB and Mongoose - Chain Search Query Helpers to Narrow Search Results: Official solution fails):

Chain Search Query Helpers