【发布时间】:2015-05-15 11:22:45
【问题描述】:
我是使用 mongoose 中间件的新手,不知道我是否很好地遵循它。这是目的。保存部门后,我想填充大学并将部门 ID 保存在大学对象中。
DepartmentSchema.post('save', function(next) {
var departmentId = this._id;
University.findOne({
_id: this.university
}, function(err, university) {
if (!university.departments) {
university.departments = [];
}
university.departments.push(new ObjectId(departmentId));
university.save(function(err) {
if (err) return console.log('err-->' + err);
// saved!
});
});
});
这工作正常,但我不确定为什么在Cascade style delete in Mongoose 他们使用了 exec() 和 next() 调用。你能告诉我这些电话的目的吗?我不知道他们在做什么,也找不到相关文件。我只是想确保我没有遗漏任何东西。
clientSchema.pre('remove', function(next) {
// 'this' is the client being removed. Provide callbacks here if you want
// to be notified of the calls' result.
Sweepstakes.remove({
client_id: this._id
}).exec();
Submission.remove({
client_id: this._id
}).exec();
next();
});
【问题讨论】:
标签: express mongoose mean-stack meanjs