The list of trips is returned by the GET /trips endpoint. It accepts no parameters:

app.get('/trips', (req, res) => { /* */ })

In the previous lesson we initialized the trips collection, so we can directly access that to get the list. We use the trips.find() method, which result we must convert to an array using toArray():

app.get('/trips', (req, res) => {
	trips.find().toArray((err, items) => {

	})
})

Then we can handle the err and items results:

app.get('/trips', (req, res) => {
	trips.find().toArray((err, items) => {
    if (err) {
      console.error(err)
      res.status(500).json({ err: err })
      return
    }
    res.status(200).json({ trips: items })
	})
})

Here’s the result of the API call in Insomnia:

Project up to now: https://glitch.com/edit/#!/node-course-project-tripcost-c?path=server.js


Go to the next lesson