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

typescript type override for find and populate #11027

Closed
vkarpov15 opened this issue Nov 29, 2021 Discussed in #11014 · 0 comments
Closed

typescript type override for find and populate #11027

vkarpov15 opened this issue Nov 29, 2021 Discussed in #11014 · 0 comments
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@vkarpov15
Copy link
Collaborator

Discussed in #11014

Originally posted by falukropp November 24, 2021
Starting with https://mongoosejs.com/docs/typescript/populate.html but using find instead of findOne, should not the following work?

import {Schema, model, Document, Types} from 'mongoose'

// `Parent` represents the object as it is stored in MongoDB
interface Parent {
  child?: Types.ObjectId
  name?: string
}
interface Child {
  name: string
}
interface PopulatedParent {
  child: Child | null
}
const ParentModel = model<Parent>(
  'Parent',
  new Schema({
    child: {type: 'ObjectId', ref: 'Child'},
    name: String
  })
)
const childSchema: Schema = new Schema({name: String})
const ChildModel = model<Child>('Child', childSchema)

// Populate with `Paths` generic `{ child: Child }` to override `child` path
ParentModel.find({})
  .populate<{child: Child}>('child')
  .orFail()
  .then(parents => {
// Property 'name' does not exist on type 'ObjectId'.
    parents.map(p => p.child.name)
  })

I get
Property 'name' does not exist on type 'ObjectId' on p.child.name

...Or do you do something else for multiple results?

This is on mongoose 6.0.13

@vkarpov15 vkarpov15 added this to the 6.0.17 milestone Nov 29, 2021
@IslandRhythms IslandRhythms added the typescript Types or Types-test related issue / Pull Request label Dec 3, 2021
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

No branches or pull requests

2 participants