Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Return mongoose document TypeScript

I’m Trying to return a mongoose document, but then i get this error:

Type ‘(IPersonDocument & { _id: ObjectId; })[]’ is missing the following properties from type ‘IPersonDocument’: firstName, lastName, groups, $assertPopulated, and 52 more.ts(2740)

IPersonDocument is extends IPerson and Mongoose.Document

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

this is my code:

repo.ts

export async function findByName(name: String): Promise<IPersonDocument>{
    return await PersonsModel.find({ firstName: name }) //problem here
}

type.ts

import { Document, Model } from "mongoose";

export interface IPerson {
    firstName: string;
    lastName: string;
    age?: number;
    email?: string,
    groups: [String],
    dateOfEntry?: Date;
}

export interface IPersonDocument extends IPerson, Document { }

export interface IPersonModel extends Model<IPersonDocument> { }

moduel.ts

import { model } from "mongoose";
import { IPersonDocument } from "../../../type/person.types";
import PersonsSchema from "./person.schema";

export const PersonsModel = model<IPersonDocument>("persons", PersonsSchema)

>Solution :

add the lean()

return await PersonsModel.find({ firstName: name }).lean();

its coavert the DOC object to js object.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading