If you are asking a question, please follow this template:
- My goal is: create a util function to get the data within last 12 months.
- My actions are:
import { Document, Model } from "mongoose";
interface MonthData {
month: string;
count: number;
}
export async function generateLast12MonthsData<T extends Document>(model: Model<T>): Promise<{ last12Months: MonthData[] }>{
const last12Months:MonthData[] = [];
const currentDate = new Date();
currentDate.setDate(currentDate.getDate() + 1);
for (let i=11; i>=0; i--) {
const endDate = new Date(
currentDate.getFullYear(),
currentDate.getMonth(),
currentDate.getDate() - i * 28,
);
const startDate = new Date(
endDate.getFullYear(),
endDate.getMonth(),
endDate.getDate() - 28,
);
const monthYear = endDate.toLocaleString(
"default",
{
day: "numeric",
month: "short",
year: "numeric",
}
);
const count = await model.countDocuments(
{
createdAt:
{
$gte: startDate,
$lt: endDate,
}
}
);
last12Months.push(
{
month: monthYear,
count,
}
);
};
return { last12Months };
}
- The result I see is:
Argument of type '{ createdAt: { $gte: Date; $lt: Date; }; }' is not assignable to parameter of type 'FilterQuery<T>'.
Type '{ createdAt: { $gte: Date; $lt: Date; }; }' is not assignable to type '{ [P in keyof T]?: Condition<T[P]> | undefined; }'.
- My expectation & question is: fix the error and understand how the error is thrown.