Fix for "Apply defaults after setting initial value" doesn't work when documents are loaded via a query #7182
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
Milestone
PR #6119 for issue #3781 addressed an issue that caused the context object (
this
) provided todefault
function callback handlers to only have access to properties set in other default handlers, not the contents of the document itself. This works when creating a document, but it does not work when loading a document via a query. This causes problems where we are trying to do conditional defaults—based on another property, the default value should either be changed or removed entirely.Another ticket was opened about this issue and closed because no code samples were provided (
#6330). I'm pretty sure this is the same issue I'm having.
The code snippet below reproduces the current behavior, with assertions for what is expected to happen. The second assertion fails because
this.type
wasundefined
in the default callback function.Tested using Node 8.11.3, mongoose 5.3.6, mongo 3.4.2.
The text was updated successfully, but these errors were encountered: