Random error when using refPath & strictPopulate on deep subdocs #15026
Labels
has repro script
There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Milestone
Prerequisites
Mongoose version
8.7.1
Node.js version
20.7.0
MongoDB server version
8.0.1
Typescript version (if applicable)
No response
Description
Some context: I have a parent model, which contains a property
child
that stores anObjectId
pointing to either achildA
orchildB
model, with the model being specified in achildType
property. The value ofchildType
is passed to therefPath
of thechild
property.childA
has a propertysubChildA
, andsubChildA
has a propertygrandChildA
.childB
has no properties.When performing a
find
onparent
and populating the entire chain ofchildren
, thepopulate
function sometimes mixes up the models, resulting in an error indicating thatchild.subChildA.grandChildA
is not in the schema.Here’s what I observed in the code of the
getModelsMapForPopulate.js
file:In
getModelsMapForPopulate
, addingconsole.log(model, options.path)
just before line 50:
return new StrictPopulate(options._fullPath || options.path)
outputs
Model { childB } subChildA.grandChildA
.Adding
options = _.cloneDeep(options)
at the beginning of this function resolves the issue.In the
addModelNamesToMap
function, replacing line 526utils.merge(currentOptions, options)
with
utils.merge(currentOptions, _.cloneDeep(options))
also resolves the issue.
It appears, then, that the problem is caused by a mutation of the
options
object passed as a parameter to the function, associated with a concurrency issue, given the random nature of the bug.Steps to Reproduce
Expected Behavior
well, it shouldn't throw :)
thanks for your help, and this wonderful project !
The text was updated successfully, but these errors were encountered: