The response of the endpoint /json should change according to the environment variable MESSAGE_STYLE

I am working on the " Basic Node and Express - Use the .env File" exercise, and I am confused by the task.
I have done the following, and the information on the browser seems to match the task’s requirement, but somehow it’s not letting me pass.

The task said to:
“Store the variable MESSAGE_STYLE=uppercase in the .env file. Then tell the GET /json route handler that you created in the last challenge to transform the response object’s message to uppercase if process.env.MESSAGE_STYLE equals uppercase . The response object should become {"message": "HELLO JSON"} .”

  1. I have created a “.env” file
  2. In the myApp.js file, I have written the following two lines of code
let msg = process.env.MESSAGE_STYLE==="uppercase"?"HELLO JSON":"Hello json";
app.get("/json",(req, res)=>(res.json({"message":msg})));

The browser returns a json string displaying:
{
“message”:“HELLO WORLD”
}

Challenge: Use the .env File

Link to the challenge:

1 Like

@edlam711,
try to understand the following codes how json works/return

res.json({key:"value"})
// the output will be  {"key":"value"}

now look at this:

let value = "myMessage";
res.json({key:value});
// here the output will be {"key":"myMessage"}

Now see this

res.json({"key":"value"})
// this will give you an error for breaking the syntax 

So, now you need to know the syntax, right?
No need to go far, if you carefully observe the above example codes, you’ll get a clear concept.

Now, check your code and make the correction if necessary.
Hope this will help you.

Thank you.

So what I get from your response is there’s a syntax error,
in particular, the key of the javascript object passed into the json method cannot be in double quotes.
But mine’s not in double quotes.

Can you be more straightforward?

Thanks

you may check your code at this point first

Sorry, my mistake. In my glitch code there’s no double quotes wrapping the key.

But the exercise still won’t let me pass.

Here’s my code. Please feel free to take a look

Can I get your .env code here?

MESSAGE_STYLE=uppercase;

Your code is running well, you may close your browser and reopen it then try frequent submit, it should work

I have checked the issue and this is what I found,

if I apply the uppercase transformation logic inside the get method, it’s passing but when make it outside it’s not passing the test, but getting the expected result.

so, for the purpose, you may apply the uppercase logic inside the get method.
Declare the variable msg just before res.json({....}) and submit few times, it’s working

Use this code

app.get(’/json’, (req, res) => {
let message = ‘Hello json’;
(process.env.MESSAGE_STYLE == ‘uppercase’) ? message=message.toUpperCase() : message=message; res.json({‘message’: message});
});

1 Like

Hello and welcome to the freeCodeCamp community~!

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.

1 Like