LearnBoost/mongoose — Issue #746: Issue with set middleware on embedded documents
Author: nkohari
Created: 2012-02-23 20:24:20+00:00
State: open

Hey, we're trying to track updates to our documents as they're applied by using `set` middleware, but we're running into a problem when creating documents. I believe the problem only occurs when you attach the middleware to embedded documents.

Here's a [simple test case](https://gist.github.com/1894855) that illustrates the problem. When you attempt to call `save()` to persist the document, it throws an exception:

```
TypeError: Cannot call method 'split' of undefined
at Schema.path (/Users/nkohari/work/set-middleware-test/node_modules/mongoose/lib/schema.js:143:25)
at EmbeddedDocument._path (/Users/nkohari/work/set-middleware-test/node_modules/mongoose/lib/document.js:537:24)
at EmbeddedDocument. (/Users/nkohari/work/set-middleware-test/node_modules/mongoose/lib/document.js:351:19)
at EmbeddedDocument. (/Users/nkohari/work/set-middleware-test/node_modules/mongoose/node_modules/hooks/hooks.js:59:24)
at /Users/nkohari/work/set-middleware-test/node_modules/mongoose/node_modules/hooks/hooks.js:52:28
at fnWrapper (/Users/nkohari/work/set-middleware-test/node_modules/mongoose/node_modules/hooks/hooks.js:158:8)
at EmbeddedDocument. (/Users/nkohari/work/set-middleware-test/test.js:17:12)
at EmbeddedDocument. (/Users/nkohari/work/set-middleware-test/node_modules/mongoose/node_modules/hooks/hooks.js:50:30)
at EmbeddedDocument.set (/Users/nkohari/work/set-middleware-test/node_modules/mongoose/node_modules/hooks/hooks.js:96:20)
at DocumentArray.cast (/Users/nkohari/work/set-middleware-test/node_modules/mongoose/lib/schema/documentarray.js:121:29)
```

The same issue occurs if you attach an empty `set` middleware, like this:

``` javascript
schema.pre('set', function(next) {
next();
});
```

Any thoughts on what might be going wrong? As best as I can tell, it seems like `Schema.path()` is being called with `undefined` as a path.

(Using Mongoose 2.5.9 installed from npm.)