Closed conduongtong closed 1 year ago
I solved by using replace refPath: 'child.type' to ref: function(child) { return child.type; }
Use ref
function instead of refPath
. As you saw, refPath
is relative to the top-level document, which makes it tricky to reuse schemas. If you use ref: function() { return this.type; }
, that's relative to the subdocument, which is better for schema reuse.
Prerequisites
Mongoose version
6.7.4
Node.js version
18.12.1
MongoDB version
6.0.2
Operating system
Windows
Operating system version (i.e. 20.04, 11.3, 10)
No response
Issue
I have schemas like this:
It works. But I want to use childSchema on another schema like this:
Because refPath nested another schema so refPath now wrong. How can I reuse childSchema?