hagopj13 / hagopj13/node-express-boilerplate
Filter by date
- Dominant language
- JavaScript
- Stars
- 7.7k
- Forks
- 2.3k
- PR merge metrics
- No merged PRs in 30d
Description
I'm trying to filter a date with $gt.
This is what I give my axios method on client side for an exact date (which works):
```
params: {
sortBy: 'name:asc',
limit: 200,
page: 1,
servedAt: `${format((new Date()),"yyyy-MM-dd'T'HH:mm:ss'Z'")}`,
},
```
But if I try to put more logic in it, it fails:
```
params: {
sortBy: 'name:asc',
limit: 200,
page: 1,
servedAt: `{$gt: ${format((new Date()),"yyyy-MM-dd'T'HH:mm:ss'Z'")} }` // <=== Invalid cast
},
```
server side looks like:
```
const getPlannedDishs = catchAsync(async (req, res) => {
const filter = pick(req.query, ['servedAt']);
const options = pick(req.query, ['sortBy', 'limit', 'page']);
const result = await dishService.queryPlannedDishs(filter, options);
res.send(result);
});
```
Error message is:
```
message: Cast to date failed for value "{$gt: 2022-10-13T00:00:00Z }" (type string) at path "servedAt" for model "PlannedDish"
at model.Query.exec (C:\Users\KingKnecht\source\repos\Mampf-KO\backend\node_modules\mongoose\lib\query.js:4498:21)
at Function.schema.statics.paginate (C:\Users\KingKnecht\source\repos\Mampf-KO\backend\src\models\plugins\paginate.plugin.js:39:54)
```
How could I achieve the filtering?
Thx
Contributor guide
Research direction
Reproduce the servedAt query from the issue, then trace the shown request handler into src/models/plugins/paginate.plugin.js and the query-building code. Determine how query operators are expected to be represented; done means a greater-than date filter reaches MongoDB without the reported cast error and the supported request syntax is clear.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- express, javascript, mongodb, node.js
- Domain
- api, backend, database
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100