Do you want to request a feature or report a bug?
Bug report
What is the current behavior?
Failing to create documents in many of my collections. Receiving the following error:
ValidationError: projects validation failed: name: this[documentIsSelected] is not a function
where projects is the collection name.
If the current behavior is a bug, please provide the steps to reproduce.
I don't have steps to reproduce. I suspect this is related to having required in the schema due to the latest changes in history.md:
fix(document): pass document to required validator so required can use arrow functions #9435 AbdelrahmanHafez
fix(document): handle required when schema has property named isSelected #9438
I'll try to provide a gist or script when I get off of work today if no one comes up with anything until then.
What is the expected behavior?
Should be able to create a document in a collection using the create function.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Mongoose: 5.10.7
MongoDB: 4.2.9
Node.js: 10.16.3
Note: I downgraded to mongoose 5.10.6 and that is working fine.
Reproduced:
https://gist.github.com/Adam-Edry/b6523c461c4017c662948869724fcdd8
gists won't allow me to have directories so I substituted _
for the directory.
We have 2 dirs test-mongoose
and depend-package
each is a node.js package.
After you go into test-mongoose, npm install
and then run node index.js
you should see the error
This seems to happen because the mongoose module being used in the dependency package is not the same one as the primary package (test-mongoose). Are there any globals used when we create a schema?
The offending line is mongoose/lib/schemaType.js:921
and logging there shows that this[documentIsSelected]
is undefined.
Let me know if I can be of further help
+ 1
Seeing this as well, when my Schema has required on one of its field (even when data is provided) validation fails.
Downgrading to 5.10.6 fixed the problem
The problem was introduced here:
https://github.com/Automattic/mongoose/commit/b128c9bf21b4d4edce09d39b0f9fbd5741f3e800#diff-020b22edc836e4a997579b91daed76b9R8-R10
exports.documentIsSelected = Symbol('mongoose#Document#isSelected');
exports.documentIsModified = Symbol('mongoose#Document#isModified');
exports.documentModifiedPaths = Symbol('mongoose#Document#modifiedPaths');
We are requiring mongoose
from two different packages so they don't share the symbols registry. That's why you get ValidationError: projects validation failed: name: this[documentIsSelected] is not a function
a.js
module.exports = Symbol('foo')
b.js
module.exports = Symbol('foo')
index.js
const fooA = require('./a')
const fooB = require('./b')
fooA === fooB // === false
Using Symbol.for
fixes the problem but I don't know if this is the best way to do it 馃.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for
Thanks for the repro and sorry for the difficulty. We worked around this issue by ensuring that documents don't end up with a schema that's tied to a different mongoose module. When you call mongoose.model()
with a schema from a different mongoose module, we'll instantiate a new schema that's tied to the mongoose
from mongoose.model()
.
As a side note, if you have a separate module that stores your schemas, you should consider listing Mongoose in peerDependencies
rather than dependencies
, so you minimize the risk of version mismatch.
Most helpful comment
+ 1
Seeing this as well, when my Schema has required on one of its field (even when data is provided) validation fails.
Downgrading to 5.10.6 fixed the problem