Basic Node and Express - Use the .env File

Hi, my code works properly on the glitch page. but it returns the following error in FCC -:

The response of "/json" does not change according to MESSAGE_STYLE

My code

let messageObject = {"message": "Hello json"};

app.get('/json', function(req, res) {
  if (process.env.MESSAGE_STYLE === 'uppercase') {
     return res.json(messageObject.message.toUpperCase());
  } else {
      return res.json(messageObject.message);
  }
});

link for glitch app
https://veil-cereal.glitch.me/

https://veil-cereal.glitch.me/json
returns
messageObject.toUpperCase is not a function

You sure messageObject.message is string type?

i had changed the code a littleā€¦ here is updated one

let messageObject = {"message": "Hello json"};

app.get('/json', function(req, res) {
  if (process.env.MESSAGE_STYLE === 'uppercase') {
     return res.json(messageObject.toUpperCase());
  } else {
      return res.json(messageObject);
  }
});

maybe because im trying to pass the to upper case function on an object the error comes

I had tried doing it another way after reading similar questions hereā€¦

but im wondering if there is a way to solve it in this set of code, directly trying to apply the toUpperCase function inside the res.json()

Iā€™m sorry I thought messageObject is not included in your code provided, sorry.

Yes, how do you uppercase an object?!

Iā€™m not sure how does res.json could create a json with just one string?

What about this?

let messageObject = {"message": "Hello json"};
app.get('/json', function(req, res) {
  if (process.env.MESSAGE_STYLE === 'uppercase') {
     var u_=JSON.parse(JSON.stringify(messageObject ));
     u_.message=u_.message.toUpperCase();
     return res.json(u_);
  } else {
      return res.json(messageObject);
  }
});
2 Likes

thanks that works alright

can you explain this line, what is its use here
var u_=JSON.parse(JSON.stringify(messageObject ));

JSON.stringify(<<json_obj>>)

turns the json object to string.

JSON.parse(<<str>>)

parse string and creates on json object.

JSON.parse(JSON.stringify(messageObject ));

means copy the messageObject object, but I think another better approach are out there, maybe u_={...messageObject } works too, donā€™t know ,have a try.

Keep going on great work, happy programming.

2 Likes

app.get(ā€™/jsonā€™,(req,res) => {
let jsonData = {ā€œmessageā€: ā€œHello jsonā€};
if(process.env.MESSAGE_STYLE == ā€œuppercaseā€) { jsonData.message = jsonData.message.toUpperCase() }
return res.json(jsonData);
}
);

4 Likes

this is another solution
app.get("/json",function(req, res) {
if(process.env.MESSAGE_STYLE == ā€˜uppercaseā€™){
res.json({ā€œmessageā€: ā€œHELLO JSONā€}); }else{
res.json({ā€œmessageā€: ā€œHello jsonā€});
}
})

1 Like

But then you you would have to right a lot of extra code for extra json.messages that didnā€™t match ā€œHello jsonā€. [designerdarpan]'s way will save you a lot of time in the future.

What do you think of this solution?

It basically creates a new object instead of updating the existing one:

app.get("/json", function(req, res) {
  let myObj = {"message": "Hello json"}
  if (process.env.MESSAGE_STYLE == "uppercase") {
    let newObj = {}
    for (let prop in myObj) {
      newObj[prop] = myObj[prop].toUpperCase();
    }
    res.json(newObj);
  } else {
    res.json(myObj);  
  }
});
2 Likes

Thank you for clearly defining a working solution! This one was tough to work through as the instructions and ā€œhintā€ were not very clear.

1 Like