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

Populate with TypeScript not working on Cursor #14374

Closed
2 tasks done
schwarmco opened this issue Feb 24, 2024 · 1 comment · Fixed by #14384
Closed
2 tasks done

Populate with TypeScript not working on Cursor #14374

schwarmco opened this issue Feb 24, 2024 · 1 comment · Fixed by #14384
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@schwarmco
Copy link

schwarmco commented Feb 24, 2024

Prerequisites

  • I have written a descriptive issue title
  • 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 MongoDB
interface Parent {
    child?: Types.ObjectId
    name?: string
}
const ParentModel = model<Parent>(
    'Parent',
    new Schema({
        child: { type: Schema.Types.ObjectId, ref: 'Child' },
        name: String
    })
)

interface Child {
    name: string
}
const childSchema: Schema = new Schema({ name: String })
const _ChildModel = model<Child>('Child', childSchema)

async function main() {
    // This works
    const result = await ParentModel.find({}).populate<{ child: Child }>('child').orFail()
    for (const doc of result) {
        const _t: string = doc.child.name
    }

    // This does not
    const cursor = ParentModel.find({}).populate<{ child: Child }>('child').cursor()
    for await (const doc of cursor) {
        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

@FaizBShah
Copy link
Contributor

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

@vkarpov15 vkarpov15 added this to the 8.2.1 milestone Feb 26, 2024
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Feb 26, 2024
vkarpov15 added a commit that referenced this issue Feb 27, 2024
types(querycursor): correct cursor async iterator type with `populate()` support
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants