I Know how to populate mongoose with javascript but as a new typescript user i dont know how to populate mongoose with typescript…I have tried to read the documentation but i don’t get i;;
import { Schema, model } from 'mongoose';
import { TAcademicDepartment } from './academicDepartment.interface';
import { TAcademicFaculty } from '../acdemicFaculty/academicFaculty.interface';
const academicDepartmentSchema = new Schema<TAcademicDepartment>(
{
name: {
type: String,
required: true,
unique: true,
},
academicFaculty: {
type: Schema.Types.ObjectId,
required: true,
ref: 'AcademicFaculty',
},
},
{
timestamps: true,
},
);
academicDepartmentSchema.pre(/^find/, async function(next) {
this.populate<{ academicFaculty: TAcademicFaculty }>('academicFaculty')
next()
})
export const AcademicDepartment = model<TAcademicDepartment>(
'AcademicDepartment',
academicDepartmentSchema,
);
export type TAcademicFaculty = {
name: string;
};
when i use the code then i got something like this
│ E This expression is not callable. typescript (2349) [45, 8]
│ Each member of the union type ‘{ <Paths = {}>(path: string | PopulateOptions | (string | PopulateOptions)[]): Promise<MergeType<Document<unknown, {}, FlatRecord<TAcademicDepartment>> & FlatRecord<…> & { …; }, Paths>>; <Paths = {}>(path: string, select?: string | … 1 more … | undefined, model?: Model<…> | undefined, match?: AnyObject | u…’ has signatures, but none of those signatures are compatible with each other.
│
I tried to understand the mongoose documentation with typescript but i cant get it..
thanks a lot for your help