Moving from array of single subdocument to mixed array - what am I missing? #13385
-
Basically, in TypeScript-lingo, I'm changing from Here's my current approach. const hydrateOptions: Hook = async (context: HookContext<IScene>) => {
context.data?.nodes?.forEach(node => {
node.options = node.options.map(option => {
if (typeof option === 'object') {
// Here's where the relevant part starts.
const hydratedOption = models.SceneOption.hydrate(option)
const error = hydratedOption.validateSync()
if (error) {
throw new Error(error)
}
return hydratedOption
// Here's where the revelant part ends.
}
return option
})
})
} So - as you can see, I'm hydrating the "options" and validating them. This works 100% as expected. So far so good. However - if I submit the same document without the if (!hydratedOption._id) {
hydratedOption._id = new mongoose.Types.ObjectId()
} But each "option" actually also contains other sub-documents and this happens there too. In short: This approach seems to no longer automatically create missing IDs. What can I do to make sure it behaves as, or as close to, the way it was when it was an array of a sub-document schema rather than a mixed array? PS. I'm using v5 of Mongoose and am currently not able to upgrade to 6 or 7. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You could try |
Beta Was this translation helpful? Give feedback.
You could try
const hydratedOption = new models.SceneOption(option); hydratedOption.validateSync();
That will also apply defaults, etc.