Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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