-
Hello, can you please help me? Here is the example code. It uses latest mongoose and typegoose. import { prop, Ref, getModelForClass, getDiscriminatorModelForClass, modelOptions } from '@typegoose/typegoose';
import * as mongoose from 'mongoose';
enum USER_TYPES {
TEACHER = 'teacher'
}
class Book {
@prop({ required: true })
name!: string;
}
@modelOptions({
schemaOptions: {
discriminatorKey: 'type'
}
})
class User {
@prop({ required: true, enum: USER_TYPES })
type!: USER_TYPES;
@prop({ required: true })
firstName!: string;
@prop({ required: true })
lastName!: string;
}
class Nested {
@prop()
a?: number;
@prop()
bookId?: mongoose.Schema.Types.ObjectId;
@prop({
localField: 'bookId',
foreignField: '_id',
justOne: true,
ref: () => Book
})
readonly book?: Ref<Book>;
}
class Teacher extends User {
@prop({ required: true })
salary!: number;
@prop({ type: () => [mongoose.Schema.Types.ObjectId] })
bookIds?: mongoose.Schema.Types.ObjectId[];
@prop({
localField: 'bookIds',
foreignField: '_id',
ref: () => Book
})
readonly books?: Ref<Book>[];
@prop({ type: Nested })
nested?: Nested;
}
const BookModel = getModelForClass(Book, {
schemaOptions: {
collection: 'books',
toJSON: { virtuals: true },
toObject: { virtuals: true }
}
});
const UserModel = getModelForClass(User, {
schemaOptions: {
collection: 'users',
toJSON: { virtuals: true },
toObject: { virtuals: true }
}
});
const TeacherModel = getDiscriminatorModelForClass(UserModel, Teacher, USER_TYPES.TEACHER, {
schemaOptions: {
toJSON: { virtuals: true },
toObject: { virtuals: true }
}
});
async function start() {
try {
await mongoose.connect('mongodb://localhost:27017', {dbName: 'test-typegoose'});
const book1 = await BookModel.create({
name: 'book1'
});
const book2 = await BookModel.create({
name: 'book2'
});
const teacher = await TeacherModel.create({
firstName: 'Vasyl',
lastName: 'Kh',
salary: 1000,
bookIds: [book1._id, book2._id],
nested: {
a: 1000,
bookId: book1._id
}
});
const qTeacher = await TeacherModel.findOne().populate('books').populate('nested.book').exec();
console.log('qTeacher', qTeacher)
process.exit(0);
} catch (err) {
console.log(err);
process.exit(1);
}
}
start(); Can you please explain what I'm doing wrong so that populate('nested.book') this populate does not work. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
to me it just looks like you dont have @modelOptions({ schemaOptions: { toJSON: { virtuals: true }, toObject: { virtuals: true } } })
class Nested {} PS: related is Automattic/mongoose#13325, and asked about explicit schemas (the case of this issue) at Automattic/mongoose#13325 (comment) |
Beta Was this translation helpful? Give feedback.
-
virtual options are not inherited by nested classes, so use on each class that is gonna use populates:
Also if you have array that is array of some class instances that use populate you should use toObject({virtuals: true}) to get populated values in console. |
Beta Was this translation helpful? Give feedback.
virtual options are not inherited by nested classes, so use on each class that is gonna use populates:
Also if you have array that is array of some class instances that use populate you should use toObject({virtuals: true}) to get populated values in console.