{"action":"created","issue":{"url":"https://api.github.com/repos/LearnBoost/mongoose/issues/2638","labels_url":"https://api.github.com/repos/LearnBoost/mongoose/issues/2638/labels{/name}","comments_url":"https://api.github.com/repos/LearnBoost/mongoose/issues/2638/comments","events_url":"https://api.github.com/repos/LearnBoost/mongoose/issues/2638/events","html_url":"https://github.com/LearnBoost/mongoose/issues/2638","id":56298229,"number":2638,"title":"Creating custom SchemaTypes (mongoose 3.8.12)","user":{"login":"kallisti-dev","id":1530244,"avatar_url":"https://avatars.githubusercontent.com/u/1530244?v=3","gravatar_id":"","url":"https://api.github.com/users/kallisti-dev","html_url":"https://github.com/kallisti-dev","followers_url":"https://api.github.com/users/kallisti-dev/followers","following_url":"https://api.github.com/users/kallisti-dev/following{/other_user}","gists_url":"https://api.github.com/users/kallisti-dev/gists{/gist_id}","starred_url":"https://api.github.com/users/kallisti-dev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kallisti-dev/subscriptions","organizations_url":"https://api.github.com/users/kallisti-dev/orgs","repos_url":"https://api.github.com/users/kallisti-dev/repos","events_url":"https://api.github.com/users/kallisti-dev/events{/privacy}","received_events_url":"https://api.github.com/users/kallisti-dev/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/LearnBoost/mongoose/labels/help","name":"help","color":"FFFFFF"}],"state":"closed","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/LearnBoost/mongoose/milestones/25","labels_url":"https://api.github.com/repos/LearnBoost/mongoose/milestones/25/labels","id":963705,"number":25,"title":"4.0.0-rc2","description":"","creator":{"login":"vkarpov15","id":1620265,"avatar_url":"https://avatars.githubusercontent.com/u/1620265?v=3","gravatar_id":"","url":"https://api.github.com/users/vkarpov15","html_url":"https://github.com/vkarpov15","followers_url":"https://api.github.com/users/vkarpov15/followers","following_url":"https://api.github.com/users/vkarpov15/following{/other_user}","gists_url":"https://api.github.com/users/vkarpov15/gists{/gist_id}","starred_url":"https://api.github.com/users/vkarpov15/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vkarpov15/subscriptions","organizations_url":"https://api.github.com/users/vkarpov15/orgs","repos_url":"https://api.github.com/users/vkarpov15/repos","events_url":"https://api.github.com/users/vkarpov15/events{/privacy}","received_events_url":"https://api.github.com/users/vkarpov15/received_events","type":"User","site_admin":false},"open_issues":2,"closed_issues":6,"state":"open","created_at":"2015-02-03T15:34:11Z","updated_at":"2015-02-06T16:27:59Z","due_on":"2015-02-09T05:00:00Z","closed_at":null},"comments":6,"created_at":"2015-02-02T21:48:33Z","updated_at":"2015-02-06T21:05:53Z","closed_at":"2015-02-06T16:27:59Z","body":"Hello! So I'm creating a custom SchemaType, following mongoose-long as a reference. However, I  encountered a problem when using my SchemaType within a SchemaArray.\r\n\r\nHere's a summary of my environment for reference\r\n* Node: 0.10.33\r\n* MongoDB: 2.4.12\r\n* mongoose: 3.8.12\r\n\r\nThe issue should be reproducible using the following code\r\n```javascript\r\n'use strict';\r\nvar mongoose = require(\"mongoose\");\r\nvar util = require(\"util\");\r\n\r\nfunction MySchemaType(path, options) {\r\n    mongoose.SchemaType.call(this, path, options, 'MySchemaType');\r\n};\r\n\r\nMySchemaType.prototype.cast = function(val, doc, init) {\r\n    console.log(\"casted\");\r\n    return val;       \r\n};\r\n\r\nutil.inherits(MySchemaType, mongoose.SchemaType);\r\n\r\nvar mySchema = new mongoose.Schema({\r\n    path: [MySchemaType]\r\n});\r\n\r\n\r\nmongoose.connect(\"mongodb://localhost:27017/mydatabase\", {\r\n    user: \"myuser\",\r\n    pass: \"1234\"\r\n});\r\n\r\nvar db = mongoose.connection;\r\n\r\ndb.once(\"open\", function() {\r\n    var MyModel = mongoose.model(\"MyModel\", mySchema);\r\n    MyModel.create( {path: 0}, function(err, obj) {\r\n        if(err) throw err;\r\n        console.log(\"object created\");\r\n        obj.path = 1;\r\n        obj.save(function(err) {\r\n            if(err) throw err;\r\n            console.log(\"saved\");\r\n        });\r\n    });\r\n});\r\n```\r\n\r\nI get the following exception when executing this code\r\n\r\n```\r\n.../node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:245\r\n        throw message;\r\n              ^\r\nCastError: Cast to undefined_method failed for value \"0\" at path \"path\"\r\n    at SchemaArray.cast (.../node_modules/mongoose/lib/schema/array.js:149:15)\r\n    at SchemaArray.cast (.../node_modules/mongoose/lib/schema/array.js:155:17)\r\n    at SchemaArray.SchemaType.applySetters (...node_modules/mongoose/lib/schematype.js:526:17)\r\n    at ...node_modules/mongoose/lib/document.js:544:18\r\n    at model.Document.$__try (.../node_modules/mongoose/lib/document.js:757:8)\r\n    at model.Document.set (.../node_modules/mongoose/lib/document.js:543:24)\r\n    at model.Document.set (.../node_modules/mongoose/lib/document.js:454:18)\r\n    at model.Document (.../node_modules/mongoose/lib/document.js:60:10)\r\n    at model.Model (.../node_modules/mongoose/lib/model.js:43:12)\r\n    at new model (.../node_modules/mongoose/lib/model.js:2502:11)\r\n```\r\n\r\nI saw that the \"real\" exception was being caught and then rethrown as a CastError. Changing line 149 of schema/array.js to be ``throw e`` to reveal the original exception, I get the following stacktrace:\r\n\r\n```\r\n...node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:245\r\n        throw message;\r\n              ^\r\nTypeError: Object #<MySchemaType> has no method 'cast'\r\n    at SchemaArray.cast (...node_modules/mongoose/lib/schema/array.js:145:34)\r\n    at SchemaArray.cast (...node_modules/mongoose/lib/schema/array.js:156:17)\r\n    at SchemaArray.SchemaType.applySetters (...node_modules/mongoose/lib/schematype.js:526:17)\r\n    at ...node_modules/mongoose/lib/document.js:544:18\r\n    at model.Document.$__try (...node_modules/mongoose/lib/document.js:757:8)\r\n    at model.Document.set (...node_modules/mongoose/lib/document.js:543:24)\r\n    at model.Document.set (...node_modules/mongoose/lib/document.js:454:18)\r\n    at model.Document (...node_modules/mongoose/lib/document.js:60:10)\r\n    at model.Model (...node_modules/mongoose/lib/model.js:43:12)\r\n    at new model (...node_modules/mongoose/lib/model.js:2502:11)\r\n```\r\n\r\nSo it would appear that MySchemaType is not being instantiated before use as a caster, but I can't pinpoint exactly where this occurs. Additionally, if I remove the following line from my original example:\r\n\r\n```javascript\r\nutil.inherits(MySchemaType, mongoose.SchemaType);\r\n```\r\n\r\nEverything works as expected:\r\n\r\n```\r\n$ node issue.js\r\ncasted\r\nobject created\r\ncasted\r\nsaved\r\n```\r\n\r\nI'm not sure if this is a bug necessarily, but it's definitely confusing for someone attempting to write their own schema types. Any clarification on why inheriting from SchemaType is causing the above error would be appreciated.\r\n"},"comment":{"url":"https://api.github.com/repos/LearnBoost/mongoose/issues/comments/73312273","html_url":"https://github.com/LearnBoost/mongoose/issues/2638#issuecomment-73312273","issue_url":"https://api.github.com/repos/LearnBoost/mongoose/issues/2638","id":73312273,"user":{"login":"vkarpov15","id":1620265,"avatar_url":"https://avatars.githubusercontent.com/u/1620265?v=3","gravatar_id":"","url":"https://api.github.com/users/vkarpov15","html_url":"https://github.com/vkarpov15","followers_url":"https://api.github.com/users/vkarpov15/followers","following_url":"https://api.github.com/users/vkarpov15/following{/other_user}","gists_url":"https://api.github.com/users/vkarpov15/gists{/gist_id}","starred_url":"https://api.github.com/users/vkarpov15/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vkarpov15/subscriptions","organizations_url":"https://api.github.com/users/vkarpov15/orgs","repos_url":"https://api.github.com/users/vkarpov15/repos","events_url":"https://api.github.com/users/vkarpov15/events{/privacy}","received_events_url":"https://api.github.com/users/vkarpov15/received_events","type":"User","site_admin":false},"created_at":"2015-02-06T21:05:53Z","updated_at":"2015-02-06T21:05:53Z","body":"Nope there's definitely a lack of documentation on that front. Any help would be most appreciated :) As a matter of fact, I don't really know the answer right now. `cast()` and `castForQuery()` are the only ones I can think of off the top of my head, but I'm not 100% sure those are the only ones. Documentation would help me too :)"}}