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 Create a New Quote

getting "TypeError: Cannot read property 'quote' of undefined" The terminal shows the error at app.js 22:23

const express = require('express');
const { process_params } = require('express/lib/router');
const app = express();

const records = require('./records');

app.use(express.json());

// Send a GET request to /quotes to READ a list of quotes 
app.get('/quotes',  async (req, res)=> { 
  const quotes =  await records.getQuotes();
  res.json(quotes);
});
// Send a GET request to /quotes/:id to READ(view) a quote
app.get('/quotes/:id', async (req, res)=> {
   const quote =  await records.getQuote(req.params.id);
    res.json(quote);
});
// Send a POST request to /quotes to CREATE a new quote 
app.post('/quotes', (res, req)=> {
   const quote = records.createQuote({               /**TERMINAL SHOWS ERROR HERE*/
      quote: req.body.quote,
      author: req.body.author
   });
   res.json(quote);
});
// Send a PUT request to /quotes/:id to UPDATE a quote
// Send a DELETE request to /quotes/:id to DELETE a quote
// Send a GET request to /quotes/quotes/random to READ (view) random quote

app.listen(3000, () => console.log('Quote API listening on port 3000!'));
TypeError: Cannot read property 'quote' of undefined
    at /Users/akeemayoade/Downloads/rest_apis_express_projectfiles/S1V4_starter_files/app.js:22:23
    at Layer.handle [as handle_request] (/Users/akeemayoade/Downloads/rest_apis_express_projectfiles/S1V4_starter_files/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/akeemayoade/Downloads/rest_apis_express_projectfiles/S1V4_starter_files/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/akeemayoade/Downloads/rest_apis_express_projectfiles/S1V4_starter_files/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/akeemayoade/Downloads/rest_apis_express_projectfiles/S1V4_starter_files/node_modules/express/lib/router/layer.js:95:5)
    at /Users/akeemayoade/Downloads/rest_apis_express_projectfiles/S1V4_starter_files/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/Users/akeemayoade/Downloads/rest_apis_express_projectfiles/S1V4_starter_files/node_modules/express/lib/router/index.js:341:12)
    at next (/Users/akeemayoade/Downloads/rest_apis_express_projectfiles/S1V4_starter_files/node_modules/express/lib/router/index.js:275:10)
    at /Users/akeemayoade/Downloads/rest_apis_express_projectfiles/S1V4_starter_files/node_modules/body-parser/lib/read.js:130:5
    at invokeCallback (/Users/akeemayoade/Downloads/rest_apis_express_projectfiles/S1V4_starter_files/node_modules/raw-body/index.js:224:16)

Error only occurs when client sends POST request (using the thunder client for testing api)

1 Answer

Rohald van Merode
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

Hi Akeem Ayoade 👋

Looks like there's an issue with the order of your req and res parameters. switching those around should fix your issue 🙂

app.post('/quotes', (req, res) => {

That was it, thank you!