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 trial

JavaScript REST APIs with Express Create, Read, Update, Delete Using Try/Catch with Async/Await

Can we use res.json(err.message); instead of res.json(message: err.message)?

att

3 Answers

I 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.

By ensuring that all the responses are formatted as JSON objects, the client application can handle the returned information in the same manner.

res.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.

Go it, thank you