You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched existing issues to ensure the bug has not already been reported
Mongoose version
8.6.0
Node.js version
20.10.0
MongoDB server version
6.0.2
Typescript version (if applicable)
5.5.4
Description
My project has a complex database schema and some document models have arrays of embedded subdocuments. Usually, to update an embedded array the set method of the parent document is used:
constparentInstance=awaitParentModel.findById(parentId);// Some codeparentInstance.set({items: [
...parentInstance.items,newItem1,newItem2,// Other items...// Or it could also be "...newItems" where newItems is an array of new embedded documents],});
Due to complex updating methods, at some points it happens that it is necessary to use one or more Array prototype methods (for instance push or splice) to build, step by step, the new embedded array subdocument, instead of using just the set method like in the previous simple example. So, in some cases, the code can be (inside a loop for instance):
constparentInstance=awaitParentModel.findById(parentId);// Some complex codeconstnewItem={/* new item fields */};// Push new itemparentInstance.items.push(newItem);// Or insert new item inside the embedded arrayconstinsertIndex=parentInstance.items.findIndex((item)=>/* some criteria */);parentInstance.items.splice(insertIndex,0,newItem);
The problem occurs on cases that are similar to the second example. The type error that occurs on methods like push and splice is the following:
Argument of type '{ /* Item props list... */ }' is not assignable to parameter of type 'Subdocument<unknown, Record<string, never>, IItemModel> & Omit<IItemModel & { _id: ObjectId; }, keyof ItemModelDocumentOverrides> & ItemModelDocumentOverrides'.
Type '{ /* Item props list... */ }' is missing the following properties from type 'Subdocument<unknown, Record<string, never>, IItemModel>': $isSingleNested, ownerDocument, parent, $parent, and 54 more. ts(2345)
Here's a screenshot of the reproduction code:
In this case, embedded document model has only a code field, thus the error is not due to missing required props.
Am I doing something wrong? Am I missing something on the model interface definition, documentOverrides, virtuals etc...?
On the reproduction link, you can find more details about how documents and embedded subdocuments are defined in my project.
Your ParentDocumentOverrides type is incorrect, parentInstance.items should be a Mongoose DocumentArray, not a vanilla JavaScript array. Your code compiles fine with the following change:
vkarpov15
added
help
This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
and removed
has repro script
There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
labels
Oct 18, 2024
Prerequisites
Mongoose version
8.6.0
Node.js version
20.10.0
MongoDB server version
6.0.2
Typescript version (if applicable)
5.5.4
Description
My project has a complex database schema and some document models have arrays of embedded subdocuments. Usually, to update an embedded array the
set
method of theparent document
is used:Due to complex updating methods, at some points it happens that it is necessary to use one or more
Array prototype methods
(for instancepush
orsplice
) to build, step by step, the new embedded array subdocument, instead of using just theset
method like in the previous simple example. So, in some cases, the code can be (inside a loop for instance):The problem occurs on cases that are similar to the second example. The type error that occurs on methods like
push
andsplice
is the following:Here's a screenshot of the reproduction code:
In this case, embedded document model has only a
code
field, thus the error is not due to missing required props.Am I doing something wrong? Am I missing something on the model interface definition, documentOverrides, virtuals etc...?
On the reproduction link, you can find more details about how documents and embedded subdocuments are defined in my project.
Steps to Reproduce
Reproduction link: Click Here
Expected Behavior
To push or insert new embedded documents into the array without type errors.
The text was updated successfully, but these errors were encountered: