-
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 ...Or do you do something else for multiple results? This is on mongoose 6.0.13 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Just noticed the same thing. I think this is likely worthy of an issue. I suspect either the docs need to be updated or the Types need to be fixed. |
Beta Was this translation helpful? Give feedback.
-
vkarpov15 created an issue for it #11027 |
Beta Was this translation helpful? Give feedback.
vkarpov15 created an issue for it #11027