Prerequisites
Mongoose version
9.2.1
Node.js version
25.4.0
MongoDB server version
not relevant
Typescript version (if applicable)
5.9.3
Description
Trying to use refPath with a function and assign a document to that property in create, it results in a undescriptive ValidationError:
ValidationError: RefPathTest validation failed: ref: Cast to ObjectId failed for value "{
propA: 'helloA',
_id: new ObjectId('699069acf888d8b513312cd9'),
__v: 0
}" (type model) at path "ref" because of "TypeError"
Upon further investigation, this is caused by a TypeError inside mongoose:
reason: TypeError: path.split is not a function
at Schema.hasMixedParent (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/schema.js:1944:25)
at Schema.path (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/schema.js:1288:23)
at model.Document.$__path (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:2034:25)
at model.Document.get (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:1943:65)
at /mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:1435:29
at model.$set (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:1437:7)
at model.$set (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:1170:16)
at model.Document (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:181:12)
at model.Model (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:131:12)
at new model (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:4805:15)
at /mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:2747:20
at Array.map (<anonymous>)
at model.create (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:2735:34)
at Object.<anonymous> (/mnt/ssd/projects/nodejs/typegoose/test/tests/ref.test.ts:484:40)
at processTicksAndRejections (node:internal/process/task_queues:104:5),
I had also tried with other property names in case they were messing with "special properties", but the error remained the same.
Finally, note that assigning a document to a refPath property that is not a function works as expected (where it gets cast to the reftype, in this case ObjectId)
Full Error
ValidationError: RefPathTest validation failed: ref: Cast to ObjectId failed for value "{
propA: 'helloA',
_id: new ObjectId('699069acf888d8b513312cd9'),
__v: 0
}" (type model) at path "ref" because of "TypeError"
at model.Document.invalidate (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:3405:32)
at model.$set (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:1522:12)
at model.$set (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:1170:16)
at model.Document (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:181:12)
at model.Model (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:131:12)
at new model (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:4805:15)
at /mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:2747:20
at Array.map (<anonymous>)
at model.create (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:2735:34)
at Object.<anonymous> (/mnt/ssd/projects/nodejs/typegoose/test/tests/ref.test.ts:484:40)
at processTicksAndRejections (node:internal/process/task_queues:104:5) {
errors: {
ref: CastError: Cast to ObjectId failed for value "{
propA: 'helloA',
_id: new ObjectId('699069acf888d8b513312cd9'),
__v: 0
}" (type model) at path "ref" because of "TypeError"
at model.$set (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:1523:9)
at model.$set (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:1170:16)
at model.Document (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:181:12)
at model.Model (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:131:12)
at new model (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:4805:15)
at /mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:2747:20
at Array.map (<anonymous>)
at model.create (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:2735:34)
at Object.<anonymous> (/mnt/ssd/projects/nodejs/typegoose/test/tests/ref.test.ts:484:40)
at processTicksAndRejections (node:internal/process/task_queues:104:5) {
stringValue: '"{\n' +
" propA: 'helloA',\n" +
" _id: new ObjectId('699069acf888d8b513312cd9'),\n" +
' __v: 0\n' +
'}"',
messageFormat: undefined,
kind: 'ObjectId',
value: [Object],
path: 'ref',
reason: TypeError: path.split is not a function
at Schema.hasMixedParent (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/schema.js:1944:25)
at Schema.path (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/schema.js:1288:23)
at model.Document.$__path (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:2034:25)
at model.Document.get (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:1943:65)
at /mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:1435:29
at model.$set (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:1437:7)
at model.$set (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:1170:16)
at model.Document (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:181:12)
at model.Model (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:131:12)
at new model (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:4805:15)
at /mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:2747:20
at Array.map (<anonymous>)
at model.create (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:2735:34)
at Object.<anonymous> (/mnt/ssd/projects/nodejs/typegoose/test/tests/ref.test.ts:484:40)
at processTicksAndRejections (node:internal/process/task_queues:104:5),
valueType: 'model'
}
},
_message: 'RefPathTest validation failed'
}
Steps to Reproduce
const childASchema = new Schema({ propA: { required: true, type: String } });
const childBSchema = new Schema({ propB: { required: true, type: String } });
const ChildAModel = mongoose.model('ChildA', childASchema);
const ChildBModel = mongoose.model('ChildB', childBSchema);
const indexes = [ChildAModel.modelName, ChildBModel.modelName];
const refPathTestSchema = new mongoose.Schema({
model: { required: true, min: 0, max: 1, type: Number },
ref: {
refPath: function () {
return indexes[this.model];
},
type: mongoose.Types.ObjectId,
},
});
const RefPathTestModel = mongoose.model('RefPathTest', refPathTestSchema);
const childAdoc = await ChildAModel.create({ propA: 'helloA' });
await ChildBModel.create({ propB: 'helloB' });
try {
const doc = await RefPathTestModel.create({ model: 0, ref: childAdoc } as any);
} catch (err) {
// necessary, as otherwise you dont get the actual error printed
console.error(err);
throw err;
}
Expected Behavior
No error, or a more descriptive error on what went wrong.
Prerequisites
Mongoose version
9.2.1
Node.js version
25.4.0
MongoDB server version
not relevant
Typescript version (if applicable)
5.9.3
Description
Trying to use
refPathwith a function and assign a document to that property increate, it results in a undescriptiveValidationError:ValidationError: RefPathTest validation failed: ref: Cast to ObjectId failed for value "{ propA: 'helloA', _id: new ObjectId('699069acf888d8b513312cd9'), __v: 0 }" (type model) at path "ref" because of "TypeError"Upon further investigation, this is caused by a
TypeErrorinside mongoose:reason: TypeError: path.split is not a function at Schema.hasMixedParent (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/schema.js:1944:25) at Schema.path (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/schema.js:1288:23) at model.Document.$__path (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:2034:25) at model.Document.get (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:1943:65) at /mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:1435:29 at model.$set (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:1437:7) at model.$set (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:1170:16) at model.Document (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:181:12) at model.Model (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:131:12) at new model (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:4805:15) at /mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:2747:20 at Array.map (<anonymous>) at model.create (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:2735:34) at Object.<anonymous> (/mnt/ssd/projects/nodejs/typegoose/test/tests/ref.test.ts:484:40) at processTicksAndRejections (node:internal/process/task_queues:104:5),I had also tried with other property names in case they were messing with "special properties", but the error remained the same.
Finally, note that assigning a document to a
refPathproperty that is not a function works as expected (where it gets cast to the reftype, in this caseObjectId)Full Error
ValidationError: RefPathTest validation failed: ref: Cast to ObjectId failed for value "{ propA: 'helloA', _id: new ObjectId('699069acf888d8b513312cd9'), __v: 0 }" (type model) at path "ref" because of "TypeError" at model.Document.invalidate (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:3405:32) at model.$set (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:1522:12) at model.$set (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:1170:16) at model.Document (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:181:12) at model.Model (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:131:12) at new model (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:4805:15) at /mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:2747:20 at Array.map (<anonymous>) at model.create (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:2735:34) at Object.<anonymous> (/mnt/ssd/projects/nodejs/typegoose/test/tests/ref.test.ts:484:40) at processTicksAndRejections (node:internal/process/task_queues:104:5) { errors: { ref: CastError: Cast to ObjectId failed for value "{ propA: 'helloA', _id: new ObjectId('699069acf888d8b513312cd9'), __v: 0 }" (type model) at path "ref" because of "TypeError" at model.$set (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:1523:9) at model.$set (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:1170:16) at model.Document (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:181:12) at model.Model (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:131:12) at new model (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:4805:15) at /mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:2747:20 at Array.map (<anonymous>) at model.create (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:2735:34) at Object.<anonymous> (/mnt/ssd/projects/nodejs/typegoose/test/tests/ref.test.ts:484:40) at processTicksAndRejections (node:internal/process/task_queues:104:5) { stringValue: '"{\n' + " propA: 'helloA',\n" + " _id: new ObjectId('699069acf888d8b513312cd9'),\n" + ' __v: 0\n' + '}"', messageFormat: undefined, kind: 'ObjectId', value: [Object], path: 'ref', reason: TypeError: path.split is not a function at Schema.hasMixedParent (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/schema.js:1944:25) at Schema.path (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/schema.js:1288:23) at model.Document.$__path (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:2034:25) at model.Document.get (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:1943:65) at /mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:1435:29 at model.$set (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:1437:7) at model.$set (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:1170:16) at model.Document (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/document.js:181:12) at model.Model (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:131:12) at new model (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:4805:15) at /mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:2747:20 at Array.map (<anonymous>) at model.create (/mnt/ssd/projects/nodejs/typegoose/node_modules/mongoose/lib/model.js:2735:34) at Object.<anonymous> (/mnt/ssd/projects/nodejs/typegoose/test/tests/ref.test.ts:484:40) at processTicksAndRejections (node:internal/process/task_queues:104:5), valueType: 'model' } }, _message: 'RefPathTest validation failed' }Steps to Reproduce
Expected Behavior
No error, or a more descriptive error on what went wrong.