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

[Bug]: Issue with count function in Mongoose model when applying filters in @adminjs/mongoose (Deprecated) #1601

Open
qurquru opened this issue Jan 11, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@qurquru
Copy link

qurquru commented Jan 11, 2024

What happened?

I'm encountering an issue with AdminJS when applying filters to a Mongoose model. The error message I'm seeing is "TypeError: this.MongooseModel.count is not a function".

Here's the relevant code from @adminjs/mongoose/resource.js:54:39:

async count(filters = null) {
    if (Object.keys(convertFilter(filters)).length > 0) {
        return this.MongooseModel.count(convertFilter(filters));
    }
    return this.MongooseModel.estimatedDocumentCount();
}

The error starts when this.MongooseModel.count(convertFilter(filters)) is called.

My @adminjs/express and mongoose packages are up to date. I've also verified that my Mongoose model is correctly defined and exported, and it's correctly imported in my AdminJS configuration.

The issue only occurs when applying filters. The data is displayed correctly without filters.

According to mongoose docs, the count function in Mongoose is deprecated and it's recommended to use countDocuments or estimatedDocumentCount instead. However, since I'm using AdminJS, I don't have direct control over this function call.

Seems like changing it to return this.MongooseModel.countDocuments(convertFilter(filters)) fixes the issue.

Please help me resolve this issue. Thank you.

Bug prevalence

Every time i apply filters.

AdminJS dependencies version

  "dependencies": {
    "@adminjs/express": "^6.1.0",
    "@adminjs/mongoose": "^4.0.0",
    "adminjs": "^7.5.2",
    "express": "^4.18.2",
    "express-session": "^1.17.3",
    "mongoose": "^8.0.3",
  }

Relevant log output

TypeError: this.MongooseModel.count is not a function
at Resource.count (node_modules/@adminjs/mongoose/lib/resource.js:54:39)
at Object.handler (node_modules/adminjs/lib/backend/actions/list/list-action.js:75:34)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async ActionDecorator.handler (node_modules/adminjs/lib/backend/decorators/action/action-decorator.js:56:19)
at async node_modules/@adminjs/express/lib/buildRouter.js:24:22
@qurquru qurquru added the bug Something isn't working label Jan 11, 2024
@dziraf
Copy link
Contributor

dziraf commented Jan 19, 2024

Seems like changing it to return this.MongooseModel.countDocuments(convertFilter(filters)) fixes the issue.

We've tried that before and countDocuments(convertFilter(filters)) actually returned wrong counts above resource's list of records, that's why we reverted back to using count. PRs that implement countDocuments properly are welcome though.

@Junjiequan
Copy link

Junjiequan commented Jan 22, 2024

import { Filter } from "adminjs";

export const convertFilter = (filter: Filter) => {
  const filters: Record<
    string | number | symbol,
    { from: string; to: string } | RegExp
  > = {};
  Object.entries(filter.filters).forEach(([key, value]) => {
    if (typeof value.value === "string") {
      const regexPattern = new RegExp(`${value.value}`, "i");
      filters[value.path] = regexPattern;
    } else {
      filters[key] = value.value;
    }
  });
  return filters;
};

This works for me so far

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants