You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched existing issues to ensure the bug has not already been reported
Mongoose version
8.2.0
Node.js version
20.x
MongoDB server version
7.05
Typescript version (if applicable)
5.3.3
Description
Applying the Paths generic parameter as described in the mongoose documentation and fetching documents in a cursor does not result in the correct type.
Steps to Reproduce
import{Schema,model,Types}from'mongoose'// `Parent` represents the object as it is stored in MongoDBinterfaceParent{child?: Types.ObjectIdname?: string}constParentModel=model<Parent>('Parent',newSchema({child: {type: Schema.Types.ObjectId,ref: 'Child'},name: String}))interfaceChild{name: string}constchildSchema: Schema=newSchema({name: String})const_ChildModel=model<Child>('Child',childSchema)asyncfunctionmain(){// This worksconstresult=awaitParentModel.find({}).populate<{child: Child}>('child').orFail()for(constdocofresult){const_t: string=doc.child.name}// This does notconstcursor=ParentModel.find({}).populate<{child: Child}>('child').cursor()forawait(constdocofcursor){const_t: string=doc.child.name// doc.child is Types.ObjectId}}
Expected Behavior
The populated child should be of type Child as defined in the Paths generic parameter
The text was updated successfully, but these errors were encountered:
As far as I can see, there is population happening inside the cursor's next() function. I think the issue might be because of wrong type return I guess.
As is visible here, the cursor() function is returning Cursor of type DocType (which is the original parent type) instead of the ResultType (which is the type of the populated doc).
@vkarpov15 Is this intended? If this is a bug which can be fixed, I'll love to create a PR for this
Prerequisites
Mongoose version
8.2.0
Node.js version
20.x
MongoDB server version
7.05
Typescript version (if applicable)
5.3.3
Description
Applying the
Paths
generic parameter as described in the mongoose documentation and fetching documents in a cursor does not result in the correct type.Steps to Reproduce
Expected Behavior
The populated
child
should be of typeChild
as defined in thePaths
generic parameterThe text was updated successfully, but these errors were encountered: