Uncaught TypeError due to circular dependencies in MongooseError #1923

kevinoid opened this Issue Feb 13, 2014 · 1 comment


None yet
2 participants

kevinoid commented Feb 13, 2014

Attempting to insert an invalid document into a collection results in the following error:

Uncaught TypeError: object is not a function
 at model.Document.invalidate (/project/node_modules/mongoose/lib/document.js:1010:32)
 at /project/node_modules/mongoose/lib/document.js:959:16
 at validate (/project/node_modules/mongoose/lib/schematype.js:610:7)
 at /project/node_modules/mongoose/lib/schematype.js:627:9
 at Array.forEach (native)
 at SchemaDate.SchemaType.doValidate (/project/node_modules/mongoose/lib/schematype.js:614:19)
 at /project/node_modules/mongoose/lib/document.js:957:9
 at process._tickCallback (node.js:415:13)

Instead, it should be returning an instance of ValidationError with a description of the validation problem. The source of the issue is that document.js requires error.js which requires error/validation.js which cyclically requires error.js before setting module.exports. This results in the require call from error.js returning the default exports object instead of the ValidationError function.

This could be fixed by setting module.exports before requiring ../error in error/validation.js (since the function is hoisted). Alternatively, error/validation.js could add the ValidationError property to MongooseError or several other approaches (as you know - I'm just throwing out suggestions to help clarify the issue).



vkarpov15 commented Apr 29, 2015

Stale issue, re-open if this is still a problem

vkarpov15 closed this Apr 29, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment