Use the .env File

I can’t get my code to pass this one. Here’s what I currently have:

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

}

Can someone help explain why this code doesn’t pass?

Thanks

EDITED TO ADD:
I set the .env variable to
MESSAGE_STYLE=uppercase

2 Likes

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums

Can you link to your entire project?

Thanks for the tip on using back ticks; I didn’t even consider how poorly formatted it would be for a reader.

Here’s the link to my project https://flowery-engineer.glitch.me

You don’t want to hard code this into your .env file. The automated tests will be setting this value.

edit: It would help if I read the instructions

Hello,

I am also trying to get pass this challenge,
mbabaian, were you able to pass it?

1 Like

Yes, I did. I was missing “return” before both res statements
i.e.–

if (process.env.MESSAGE_STYLE==‘uppercase’){
app.get(’/json’, function(req, res) {
return res.json({ “message”: “HELLO JSON” })
});
}
else {
app.get(’/json’, function(req, res) {
 return res.json({ “message”: “Hello json” })
});

}

If that doesn’t work for you, let me know and I’ll go compare it more closely to my code.

And you do need to set MESSAGE_STYLE=‘uppercase’ in .env

Still not working… i even tried pasting your code, maybe its a beta fault, i’ll attempt it agagin later

Can you post a link to your code?

below is my code

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

You’ve got your app.get(…) section outside of your if statement. Try putting it inside the brackets of the if statement.

the logic is to only change the string to upper case if the env==uppercase
hence the default would print out as lower case.

it still doesn’t work… i tried your code and it timed out lol

the same code worked for the previous challenge…

do you have your code on glitch or some other place i can look
at it?

https://glitch.com/edit/#!/efficient-dill?path=myApp.js:10:0

I made a remix of your glitch and changed several things. It appears to be working now. https://burly-accordion.glitch.me

Please look it over carefully and compare it to what you have to find the changes I made. Also, make sure you have process.env.MESSAGE_STYLE=‘uppercase’ in the env file.

I did notice that you still had

return res.json({"message": message.toUpperCase()})

outside of your IF statement. And you had commented out some code that was necessary to complete the task; I added that code back in as well as put it in the right spots. If you go back and look at the boilerplate code, there are specific areas for each exercise.

I hope this helps. Let me know if you need anything else.

Thank you so much, i will comb through them this afternoon. i am researching and trying to pick an editor as well as learning github, as i want to start playing with them on the projects, any advice?

I think Atom is pretty good but I had issues with it slowing way down when I had large Python programs. So I switched to Visual Studio code because it can handle Python and JS. If you’re just doing HTML/CSS/JS, you should be good with Atom. Maybe you can start a new topic and ask for suggestions because there are far better coders out there who have more experience with editors.

This worked for me.

app.get('/json', (req, res) => {
  process.env.MESSAGE_STYLE === 'uppercase' ? 
    res.json({ "message": "HELLO JSON" }):
    res.json({ "message": "Hello json" })
})
10 Likes