I have a restaurant reservations project that when you type in a query string, for example: http://localhost:5000/reservations?2020-12-31
it will return with the given reservations on that day.
I am using Knex to perform my queries and I have the first function which will return all reservations from the database:
const listReservations = () => knex('reservations').select('*');
Then I am trying to take these, and return the specified reservations:
const list = async (req, res) => { const response = await service.listReservations(); const data = response.filter((reserve) => { return Date.parse(reserve.reservation_date) - Date.now() > 0; }); console.log(data); res.json({ data }); };
The data looks like:
[ { "first_name": "Rick", "last_name": "Sanchez", "mobile_number": "202-555-0164", "reservation_date": "2020-12-31", "reservation_time": "20:00:00", "people": 6, "created_at": "2020-12-10T08:30:32.326Z", "updated_at": "2020-12-10T08:30:32.326Z" }, { "first_name": "Frank", "last_name": "Palicky", "mobile_number": "202-555-0153", "reservation_date": "2020-12-30", "reservation_time": "20:00", "people": 1, "created_at": "2020-12-10T08:31:32.326Z", "updated_at": "2020-12-10T08:31:32.326Z" }, { "first_name": "Bird", "last_name": "Person", "mobile_number": "808-555-0141", "reservation_date": "2020-12-30", "reservation_time": "18:00", "people": 1, "created_at": "2020-12-10T08:31:32.326Z", "updated_at": "2020-12-10T08:31:32.326Z" }, { "first_name": "Tiger", "last_name": "Lion", "mobile_number": "808-555-0140", "reservation_date": "2025-12-30", "reservation_time": "18:00", "people": 3, "created_at": "2020-12-10T08:31:32.326Z", "updated_at": "2020-12-10T08:31:32.326Z" }, { "first_name": "Anthony", "last_name": "Charboneau", "mobile_number": "620-646-8897", "reservation_date": "2026-12-30", "reservation_time": "18:00", "people": 2, "created_at": "2020-12-10T08:31:32.326Z", "updated_at": "2020-12-10T08:31:32.326Z" } ]
If I parse my date, subtract today’s date and return the dates above 0, I get: Tiger and Anthony. If I return the ones below 0 I get the first three names.
The test case is passing in 2020-12-31 as a query string and is expecting Rick. How do I perform a GET request of the given query strings?
Answer
In Express, you can use req.query
to access the query parameters of the url (everything after the ?
). But this will only work if your query string is formatted correctly. It needs to have formatting like this: ?field=value&foo=bar&date=2020-12-31
.
Once you have a url with a query formatted this way, Express can parse it and you can use it like this:
knex('reservations').where({ 'reservation_date': req.query.date })
That is the recommended solution (plus some parameterization refactoring that I leave as an exercise for the reader). But if changing the format of the query string is not an option, then you could instead do some string processing on req.originalUrl
, like this:
const indexOfQs = req.originalUrl.indexOf('?') const date = indexOfQs === -1 ? null : req.originalUrl.substr(indexOfQs + 1) knex('reservations').where({ 'reservation_date': date })