adminjs
adminjs copied to clipboard
[Bug]: Issue with count function in Mongoose model when applying filters in @adminjs/mongoose (Deprecated)
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
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.
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