Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embedded Documents don't have methods defined on their schema #249

Closed
lukegalea opened this issue Feb 15, 2011 · 7 comments
Closed

Embedded Documents don't have methods defined on their schema #249

lukegalea opened this issue Feb 15, 2011 · 7 comments

Comments

@lukegalea
Copy link

var ObservationSchema = new Schema({
  an_array:  { type: Array , default: ['a'] }  
});

ObservationSchema.method('a_method', function() {
  return 'something';
});

mongoose.model('Observation', ObservationSchema);
var Observation = mongoose.model('Observation');

var ContainerSchema = new Schema({
  observations: [ObservationSchema]
});

mongoose.model('Container', ContainerSchema);
var Container = mongoose.model('Container');

var o = new Observation();
console.log(o.a_method()); // This works

var c = new Container();
c.observations.push({});

console.log(c.observations[0].a_method()); // This doesn't
@lukegalea
Copy link
Author

Could anyone point me in the right direction to a fix for this? I'm happy to try and fix it myself.. My code is totally dead in the water right now without this working.

Thanks in advance!

@lukegalea
Copy link
Author

Ahh.. belay that. It looks like you can use virtuals and gets.. but methods defined via "method" don't work. So I can work around it for now. Thanks.

@justmoon
Copy link
Contributor

Here is a patch:

diff --git a/lib/mongoose/schema/documentarray.js b/lib/mongoose/schema/documentarray.js
index 31fb3f8..6ce7665 100644
--- a/lib/mongoose/schema/documentarray.js
+++ b/lib/mongoose/schema/documentarray.js
@@ -29,6 +29,10 @@ function DocumentArray (key, schema, options) {
   EmbeddedDocument.prototype.schema = schema;
   EmbeddedDocument.schema = schema;

+  // apply methods
+  for (var i in schema.methods)
+    EmbeddedDocument.prototype[i] = schema.methods[i];
+
   ArrayType.call(this, key, EmbeddedDocument, options);

   var self = this;

Performance-wise this could be improved by creating the EmbeddedDocument during module compilation.

@bentruyman
Copy link

+1 to see this get fixed.

Instead of patching DocumentArray, I've been doing the following until the fix (hopefully) lands:

embeddedDoc.__proto__._schema.methods.myMethod.apply(embeddedDoc, [args]);

bnoguchi added a commit that referenced this issue Jun 9, 2011
@jimpo
Copy link

jimpo commented Oct 27, 2012

This seems to no longer work.

@aheckmann
Copy link
Collaborator

@jep37 please open a new ticket with a failing test case. include which mongoose version in use

@jimpo
Copy link

jimpo commented Nov 4, 2012

Nevermind, I didn't realize you had to define the methods on the embedded schema before defining the parent schema.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants