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"} .”
I have created a “.env” file
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”
}
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.
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
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.