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 Edit A Quote

Patrick Perkins
Patrick Perkins
14,619 Points

Getting 404 in Postman on PUT request.

The route is valid in Postman on a GET request because the json quote is returned. However, when changing to a PUT request and sending json in the body, I get a 404 error but am not receiving the error message "Quote not found" from the code.

Here is my code:

app.put('quotes/:id', async (req,res) => {
  try {
    const quote = await records.getQuote(req.params.id);
    if (quote) {
      quote.quote = req.body.quote;
      quote.author = req.body.author;

      await records.updateQuote(quote);
      res.status(204).end();
    } else {
      res.status(404).json({message: "Quote not found"})
    }
    records.updateQuote(quote)
  } catch (err) {
    res.status(500).json({message: err.message});
  }
})
Patrick Perkins
Patrick Perkins
14,619 Points

Gotta love coding. I noticed I didn't place a "/" in front of my route.

2 Answers

app.put('/quotes/:id', async (req,res) => { // you were missing the '/' before quotes
  try {
    const quote = await records.getQuote(req.params.id);
    if (quote) {
      quote.quote = req.body.quote;
      quote.author = req.body.author;

      await records.updateQuote(quote);
      res.status(204).end();
    } else {
      res.status(404).json({message: "Quote not found"})
    }
    records.updateQuote(quote)
  } catch (err) {
    res.status(500).json({message: err.message});
  }
})
Pablo Calvo
Pablo Calvo
13,044 Points

Same happened here... lol, thanks, been 3 days stuck looking at it