Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Path generic #13117

Merged
merged 4 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions test/types/populate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,34 @@ function gh12136() {
}

}

async function gh13070() {
interface IParent {
name: string;
child: Types.ObjectId;
}
interface IChild {
name: string;
parent: Types.ObjectId;
}

const parentSchema = new Schema(
{
name: { type: String, required: true },
child: { type: Schema.Types.ObjectId, ref: 'Child', required: true }
});

const childSchema = new Schema(
{
name: { type: String, required: true },
parent: { type: Schema.Types.ObjectId, ref: 'Parent', required: true }
});

const parent = model<IParent>('Parent', parentSchema);
const child = model<IChild>('Child', childSchema);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We always use capitalized names for models


const doc = await parent.findOne();
await child.populate<{child: IChild}>(doc, 'child');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't actually test that the result doc is correctly populated. Missed this:

parents.map(p => p.child.name);



}
3 changes: 2 additions & 1 deletion types/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ declare module 'mongoose' {
callback?: Callback<(HydratedDocument<T, TMethodsAndOverrides, TVirtuals>)[]>): Promise<Array<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>>;
populate(doc: any, options: PopulateOptions | Array<PopulateOptions> | string,
callback?: Callback<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>): Promise<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>;

populate<Paths={}>(docs: Array<any>, options: PopulateOptions | Array<PopulateOptions> | string): Promise<MergeType<this, Paths>>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you had actually tested the populate doc result, you'd see that this is the wrong type to use here. In this context, this is the Model class, not the document. Use the other populate() calls on Model as an example for how to do this correctly.

populate<Paths={}>(docs: any, options: PopulateOptions | Array<PopulateOptions> | string, callback?: Callback<MergeType<this, Paths>>): void;

/** Casts and validates the given object against this model's schema, passing the given `context` to custom validators. */
validate(callback?: CallbackWithoutResult): Promise<void>;
Expand Down