I'm trying to get nested subdocuments array using Typegoose.
Before refactoring with Typegoose, I had this working code with mongoose :
Interface :
export interface IFamily extends Document {
name: string;
products: IProduct[];
}
Schema :
const familySchema: Schema = new Schema({
name: { type: String },
products: [{ type: Schema.Types.ObjectId, ref: 'Product' }]
});
When I did Family.findById('5f69aa0a56ca5426b44a86c5'), I had an array of Product ObjectId in my JSON result.
After refoctoring, I use Typegoose :
Class :
@modelOptions({ schemaOptions: { collection: 'families' } })
export class Family {
@prop({ type: Schema.Types.ObjectId })
public _id?: string;
@prop({ type: String, required: false })
public name?: string;
@prop({ ref: () => Product, required: true, default: [] })
public products!: Product[];
}
When I do :
getModelForClass(Family).findById('5f69aa0a56ca5426b44a86c5')
property "products" with an array of ObjectId is not in the result (property is absent) :
{
"_id": "5f69aa0a56ca5426b44a86c5",
"name": "Fourniture"
}
I can't figue how to make that working. I think the problem is in the family class @prop(ref). I saw some example where peoples used @arrayProp but now is deprecated.
I find documentation about ref with a simple object but not with an array of object with version 5.9.1 of Typegoose.
Thanks
aside from using an old version of typegoose with new "syntax"
this is how it is supposed to look in typegoose (7.4)