是否有任何方法来定义/修改接口或类型,以 * Assert * 文档将使用mongoose填充到typescript中?
例如
interface ISchool {
name: string;
address; string;
}
interface IPerson {
name: string;
school?: PopulatedDoc<ISchool & Document>
}
const PersonSchema: Schema<IPerson> = new Schema<IPerson>({
name: { type: String },
school: { type: Schema.Types.ObjectId, ref: 'School' }
})
const Person = mongoose.model<IPerson>('Person', PersonSchema);
export default Person
然后,本质上,如果我们与Person文档交互,就没有办法知道school
属性是否被填充。例如。
const person = await Person.findOne({});
if (person.school) { . . . } // is `school` an ObjectId or a Document?
和
const person = await Person.findOne({}, undefined, { populate: { path: 'school', model: 'School'} });
if (person.school) { . . . } // is `school` an ObjectId or a Document?
是否有任何方法可以Assert文档属性已被填充?
谢谢
1条答案
按热度按时间xiozqbni1#
从populate-with-typescript文档中,我们可以:
将泛型参数
Paths
添加到populate()