Open qurquru opened 6 months ago
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
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
:The error starts when
this.MongooseModel.count(convertFilter(filters))
is called.My
@adminjs/express
andmongoose
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 usecountDocuments
orestimatedDocumentCount
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
Relevant log output