@@ -22,7 +22,6 @@ const castUpdate = require('./helpers/query/castUpdate');
2222const clone = require ( './helpers/clone' ) ;
2323const getDiscriminatorByValue = require ( './helpers/discriminator/getDiscriminatorByValue' ) ;
2424const helpers = require ( './queryHelpers' ) ;
25- const immediate = require ( './helpers/immediate' ) ;
2625const internalToObjectOptions = require ( './options' ) . internalToObjectOptions ;
2726const isExclusive = require ( './helpers/projection/isExclusive' ) ;
2827const isInclusive = require ( './helpers/projection/isInclusive' ) ;
@@ -2248,6 +2247,9 @@ Query.prototype.error = function error(err) {
22482247 */
22492248
22502249Query . prototype . _unsetCastError = function _unsetCastError ( ) {
2250+ if ( this . _error == null ) {
2251+ return ;
2252+ }
22512253 if ( this . _error != null && ! ( this . _error instanceof CastError ) ) {
22522254 return ;
22532255 }
@@ -2291,9 +2293,9 @@ Query.prototype.mongooseOptions = function(v) {
22912293
22922294Query . prototype . _castConditions = function ( ) {
22932295 let sanitizeFilterOpt = undefined ;
2294- if ( this . model != null && utils . hasUserDefinedProperty ( this . model . db . options , ' sanitizeFilter' ) ) {
2296+ if ( this . model ?. db . options ?. sanitizeFilter != null ) {
22952297 sanitizeFilterOpt = this . model . db . options . sanitizeFilter ;
2296- } else if ( this . model != null && utils . hasUserDefinedProperty ( this . model . base . options , ' sanitizeFilter' ) ) {
2298+ } else if ( this . model ?. base . options ?. sanitizeFilter != null ) {
22972299 sanitizeFilterOpt = this . model . base . options . sanitizeFilter ;
22982300 } else {
22992301 sanitizeFilterOpt = this . _mongooseOptions . sanitizeFilter ;
@@ -2536,13 +2538,12 @@ Query.prototype.collation = function(value) {
25362538 * @api private
25372539 */
25382540
2539- Query . prototype . _completeOne = function ( doc , res , callback ) {
2541+ Query . prototype . _completeOne = function ( doc , res , projection , callback ) {
25402542 if ( ! doc && ! this . options . includeResultMetadata ) {
25412543 return callback ( null , null ) ;
25422544 }
25432545
25442546 const model = this . model ;
2545- const projection = clone ( this . _fields ) ;
25462547 const userProvidedFields = this . _userProvidedFields || { } ;
25472548 // `populate`, `lean`
25482549 const mongooseOptions = this . _mongooseOptions ;
@@ -2643,7 +2644,7 @@ Query.prototype._findOne = async function _findOne() {
26432644 // don't pass in the conditions because we already merged them in
26442645 const doc = await this . mongooseCollection . findOne ( this . _conditions , options ) ;
26452646 return new Promise ( ( resolve , reject ) => {
2646- this . _completeOne ( doc , null , ( err , res ) => {
2647+ this . _completeOne ( doc , null , options . projection , ( err , res ) => {
26472648 if ( err ) {
26482649 return reject ( err ) ;
26492650 }
@@ -3238,7 +3239,7 @@ function completeOne(model, doc, res, options, fields, userProvidedFields, pop,
32383239
32393240 function _init ( err , casted ) {
32403241 if ( err ) {
3241- return immediate ( ( ) => callback ( err ) ) ;
3242+ return callback ( err ) ;
32423243 }
32433244
32443245
@@ -3251,12 +3252,12 @@ function completeOne(model, doc, res, options, fields, userProvidedFields, pop,
32513252 } else {
32523253 res . value = null ;
32533254 }
3254- return immediate ( ( ) => callback ( null , res ) ) ;
3255+ return callback ( null , res ) ;
32553256 }
32563257 if ( options . session != null ) {
32573258 casted . $session ( options . session ) ;
32583259 }
3259- immediate ( ( ) => callback ( null , casted ) ) ;
3260+ callback ( null , casted ) ;
32603261 }
32613262}
32623263
@@ -3465,7 +3466,7 @@ Query.prototype._findOneAndUpdate = async function _findOneAndUpdate() {
34653466 const doc = ! options . includeResultMetadata ? res : res . value ;
34663467
34673468 return new Promise ( ( resolve , reject ) => {
3468- this . _completeOne ( doc , res , ( err , res ) => {
3469+ this . _completeOne ( doc , res , options . projection , ( err , res ) => {
34693470 if ( err ) {
34703471 return reject ( err ) ;
34713472 }
@@ -3561,7 +3562,7 @@ Query.prototype._findOneAndDelete = async function _findOneAndDelete() {
35613562 const doc = ! includeResultMetadata ? res : res . value ;
35623563
35633564 return new Promise ( ( resolve , reject ) => {
3564- this . _completeOne ( doc , res , ( err , res ) => {
3565+ this . _completeOne ( doc , res , options . projection , ( err , res ) => {
35653566 if ( err ) {
35663567 return reject ( err ) ;
35673568 }
@@ -3715,7 +3716,7 @@ Query.prototype._findOneAndReplace = async function _findOneAndReplace() {
37153716
37163717 const doc = ! includeResultMetadata ? res : res . value ;
37173718 return new Promise ( ( resolve , reject ) => {
3718- this . _completeOne ( doc , res , ( err , res ) => {
3719+ this . _completeOne ( doc , res , options . projection , ( err , res ) => {
37193720 if ( err ) {
37203721 return reject ( err ) ;
37213722 }
0 commit comments