Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter by date #225

Open
KingKnecht opened this issue Oct 16, 2022 · 3 comments
Open

Filter by date #225

KingKnecht opened this issue Oct 16, 2022 · 3 comments

Comments

@KingKnecht
Copy link

KingKnecht commented Oct 16, 2022

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

@tuminzee
Copy link

tuminzee commented Feb 2, 2023

I am facing the same issue @KingKnecht did you find the solution to this?

@KingKnecht
Copy link
Author

@tuminzee Sorry, for late response. To be honest I don't remember if I got it running or not. But when I check my code I find the following in my controller.js

const getPlannedDishs = catchAsync(async (req, res) => {
  const filterParams =  pick(req.query, ['from', 'to']);
  const dateRangeFilter = {
    "$gte" : filterParams.from,
    "$lte" : filterParams.to,
  }
  const mongooseFilter = {
    servedAt: dateRangeFilter
  }
  //const o = {servedAt: {"$gt":"2022-10-10T00:00:00Z"}}
  const options = pick(req.query, ['sortBy', 'limit', 'page', 'populate']);
  const result = await plannedDishService.queryPlannedDishs(mongooseFilter, options);

  res.send(result);
});

and in my service.js

 const queryPlannedDishs = async (filter, options) => {
  const plannedDishes = await PlannedDish.paginate(filter, options);
  
  console.log(JSON.stringify(plannedDishes));

  return plannedDishes;
};

So, for me it looks like I have provided extra parameters on the front-end, extracted my filter params and build a new one on the backend side.

@tuminzee
Copy link

tuminzee commented Mar 2, 2023

Thank you for your response I will try it out @KingKnecht

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants