Subdocuments Array has not id() and remove()-method
Closed this issue · 4 comments
Is there an existing issue for this?
- I have searched the existing issues
Current behavior
If I have a schema with a subdocument array schema I can't access a subdocument via id()
-function or remove it with the remove()
-function
Minimum reproduction code
https://gist.github.com/gerritlo/bd78ed72c8612e0a8662a81361952b7a
Steps to reproduce
No response
Expected behavior
On subdocuments array-property of the parent document an id()
function is existing
Mongoose docs
Package version
9.2.1
mongoose version
6.9.1
NestJS version
9.3.8
Node.js version
16.15.0
In which operating systems have you tested?
- macOS
- Windows
- Linux
Other
No response
Added a mongoose example that works as expected to the gist
This is how Typescript works. You say that payments
is of type PaymentDetails[]
and you're surprised when there are not mongoose related methods even though your class doesn't have those mongoose methods or extend the mongoose types. Probably you should be using PaymentDetailsDocument[]
instead, so that it is your type plus the mongoose document type
Posted the solution that works for me here: https://stackoverflow.com/questions/75756488/how-to-push-subdocument-to-mongoose-model-with-typescript/75778521#75778521
Add a second hydrated document that represents the list:
export type PaymentDetailsDocuments = HydratedArraySubdocuments<PaymentDetails[]>;
Then, use it instead of PaymentDetails[]
:
@Prop([PaymentDetailsSchema])
payments: PaymentDetailsDocuments;
Everything should work now.