From 0890643baacbdd12e61f45ab4d265b18b68fac79 Mon Sep 17 00:00:00 2001 From: Ichiroku Date: Mon, 30 Dec 2024 14:52:05 +0100 Subject: [PATCH 1/2] Fix document not applying manual populate when using a function in schema.options.ref --- lib/document.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/document.js b/lib/document.js index 14a33ef323..9e309de87f 100644 --- a/lib/document.js +++ b/lib/document.js @@ -1386,7 +1386,9 @@ Document.prototype.$set = function $set(path, val, type, options) { const model = val.constructor; // Check ref - const ref = schema.options.ref; + const refOpt = typeof schema.options.ref === 'function' ? schema.options.ref.bind(this)() : schema.options.ref; + + const ref = refOpt?.modelName || refOpt; if (ref != null && (ref === model.modelName || ref === model.baseModelName)) { return true; } From 305997a1514464d0048b891a4742df18ea2e4892 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sat, 4 Jan 2025 13:00:37 -0500 Subject: [PATCH 2/2] use more consistent check for ref function --- lib/document.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/document.js b/lib/document.js index 9e309de87f..59676e5f8c 100644 --- a/lib/document.js +++ b/lib/document.js @@ -55,6 +55,7 @@ const documentIsModified = require('./helpers/symbols').documentIsModified; const documentModifiedPaths = require('./helpers/symbols').documentModifiedPaths; const documentSchemaSymbol = require('./helpers/symbols').documentSchemaSymbol; const getSymbol = require('./helpers/symbols').getSymbol; +const modelSymbol = require('./helpers/symbols').modelSymbol; const populateModelSymbol = require('./helpers/symbols').populateModelSymbol; const scopeSymbol = require('./helpers/symbols').scopeSymbol; const schemaMixedSymbol = require('./schema/symbols').schemaMixedSymbol; @@ -1386,7 +1387,7 @@ Document.prototype.$set = function $set(path, val, type, options) { const model = val.constructor; // Check ref - const refOpt = typeof schema.options.ref === 'function' ? schema.options.ref.bind(this)() : schema.options.ref; + const refOpt = typeof schema.options.ref === 'function' && !schema.options.ref[modelSymbol] ? schema.options.ref.call(this, this) : schema.options.ref; const ref = refOpt?.modelName || refOpt; if (ref != null && (ref === model.modelName || ref === model.baseModelName)) {