Quote API project/Express Lesson - why the bait and switch?

Hello,
I went through the entire Express lesson and completed the Quote API project as seen here:

https://www.codecademy.com/paths/back-end-engineer-career-path/tracks/becp-build-a-back-end-with-node-express-js/modules/challenge-project-quote-api/projects/quote-api

I am just wondering after completing the lessons, why we would format the body of our res.send as {quote: getRandomElement(quotes)};. This was never used in our lesson, so I’m not sure what is it doing.

//why is {}  inside here? - because it's json object and already stringified???
app.get('/api/quotes/random', (req, res, next) => {
    res.send({ 
      quote: getRandomElement(quotes)
    });    
});

My brain tells me from the lesson that I want to put in something like the below, but it doesn’t work…

app.get('/api/quotes/random', (req, res) => {  //why not something like this?
  const randomQuotes = quotes[req.params];
      res.send(randomQuotes);
   });

Thank you,

Austin

Hello, you can find the definition of the getRandomElement() function in the utils.js file:

const getRandomElement = arr => {
  if (!Array.isArray(arr)) throw new Error('Expected an array');
  return arr[Math.floor(Math.random() * arr.length)];
}

So you can replicate that same functionality without the use of the helper function if you’d like. You’ve probably used the same technique in other projects before, such as the Mixed Messages project from the JavaScript portion of the path.

I think their only intention for the switch-up was so that you can focus on creating the routes and to provide a nice error message about it needing to be an array if the helper function wasn’t being used correctly.

1 Like

Thanks for the feedback. I’m not so concerned about the function inside itself, but rather the shape/format of incuding {} inside of res.send().

I think anonymous object is what I was looking for.

Ah, yes, I misunderstood what your concerns were. My apologies. Now I see.

The front-end is expecting an object that contains the property quote, which is also an object. The project step outlined the following format

{
  quote: {/* quote object */}
}

So that res.send() call was just adhering to that specified format. In some of the previous projects and/or lessons you were using the same method of sending information but they sometimes used a different format or abstracted it away by providing helper functions.

Since it wants an object with a key of quote that has a value of the quote object from the quotes as outlined above, then any of these would work:

app.get('/api/quotes/random', (req, res) => {
    res.send({ 
      quote: getRandomElement(quotes)
    });    
});
app.get('/api/quotes/random', (req, res) => {
  const quote = getRandomElement(quotes);
  res.send({ quote });
});
app.get('/api/quotes/random', (req, res) => {
  const quote = getRandomElement(quotes);
  res.send({ quote: quote });
});
app.get('/api/quotes/random', (req, res) => {
  const randomQuote = { quote: getRandomElement(quotes) };
  res.send(randomQuote);
});

and any of the getRandomElement(quotes) calls could be replaced with quotes[Math.floor(Math.random() * quotes.length)]

1 Like

That’s great - thank you for your post.

I did see that shape was included in the instructions, but wasn’t sure why we were using it and not the stuff we had learned.

Thank you again.

1 Like