The last piece of the puzzle is getting the expenses.
We need to fill the /expenses
endpoint stub, the last one missing:
app.get('/expenses', (req, res) => {
/* */
})
This endpoint accepts the trip
parameter, which like in the last lesson when we added an expense, it’s the _id
property of a trip stored in the database.
app.get('/expenses', (req, res) => {
expenses.find({trip: req.body.trip}).toArray((err, items) => {
if (err) {
console.error(err)
res.status(500).json({ err: err })
return
}
res.status(200).json({ trips: items })
})
})
This Insomnia screenshot shows it at work:
You can find the complete code available at https://glitch.com/edit/#!/node-course-project-tripcost-e?path=server.js