Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialHanwen Zhang
20,084 PointsCan we use res.json(err.message); instead of res.json(message: err.message)?
att
3 Answers
Joseph Lander
Full Stack JavaScript Techdegree Graduate 27,765 PointsI see you get the same information back technically but I guess the method they suggest is in a proper, conventional JSON format.
try {
throw new Error("Ooopsies!");
...
} catch (err) {
res.json(err.message);
// expected output: "Ooopsies!"
}
try {
throw new Error("Ooopsies!");
...
} catch (err) {
res.json({ message: err.message });
// expected output: {"message": "Ooopsies!"}
}
JSON comes in curly braces with both key and value in double quotes.
Brian Wright
6,770 PointsBy ensuring that all the responses are formatted as JSON objects, the client application can handle the returned information in the same manner.
Michael Knott
2,041 Pointsres.json(message: err.message) means we're assigning err.message to a key called message, in the res.json object:
On success: res.json { quote: "blahblahblah" }
On error: res.json { message: err.message }
It just makes it easier to obtain the error message later.
Hanwen Zhang
20,084 PointsGo it, thank you