Automattic / Automattic/mongoose
Subdocument doesn't hold a model attribute
- Dominant language
- JavaScript
- Stars
- 27.5k
- Forks
- 4k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 35
Description
I wrote a code using the mongoose documentation .
Wanted to use model in 'pre' 'save' in order to update other collection.
Unfortunately, 'model' attribute doesn't exist in sub-document.
for the following code:
```
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var childSchema = new Schema({ name: 'string' });
var parentSchema = new Schema({
children: [childSchema]
})
childSchema.pre('save', function(next) {
console.log("childSchema pre save model " + this.model)
next()
});
parentSchema.pre('save', function(next) {
console.log("parentSchema pre savemodel " + this.model)
next()
});
var Parent = mongoose.model('Parent', parentSchema);
var parent = new Parent({ children: [{ name: 'Matt' }, { name: 'Sarah' }] })
parent.children[0].name = 'Matthew';
parent.save();
```
the output of the code:
```
childSchema pre save model undefined
childSchema pre save model undefined
parentSchema pre savemodel function model(name) {
return this.db.model(name);
}
```
Contributor guide
Research direction
Reproduce the supplied parent and child schema example first, then search the repository for subdocument save middleware and model handling. Check how `pre('save')` hooks are invoked for subdocuments versus parent documents. Done means the reported behavior is resolved or documented clearly, with coverage for the supplied example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, mongodb, node.js
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100