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
jpvajda
December 16, 2019, 6:17pm
12
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