Use the .env File

Use the .env File
0

#1

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

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.

Can you link to your entire project?


#3

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


#4

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


#5

Hello,

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


#6

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.


#7

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


#8

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


#9

Can you post a link to your code?


#10

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);
      });

#11

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


#12

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


#13

the same code worked for the previous challenge…


#14

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


#15

#16

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


#17

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.


#18

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?


#19

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.


#20

This worked for me.

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