Tuesday, 15 July 2014

node.js - Middleware defined before model definition, cascade delete failing, appears middleware gets bypassed -



node.js - Middleware defined before model definition, cascade delete failing, appears middleware gets bypassed -

this have in file. want when author document removed, book documents should removed. experimented serial middleware error handlers wired in no errors beingness logged, author beingness removed books not.

then tried parallel middleware under assumption remove() won't triggered until pre-middleware has completed doesn't seem case. author still beingness removed books not, , no errors beingness logged:

//... var book = require('./book-model''); authorschema.pre('remove', true, function(next, done) { book.remove({author: this._id}, function(err) { if (err) { console.log(err); done(err); } done(); }); next(); }); authorschema.statics.deleteauthor = function(authorid, callback) { var author = mongoose.model('author'); author.remove({_id: authorid}, callback); }; // ... module.exports = mongoose.model('author', authorschema);

so i'm thinking middleware beingness bypassed otherwise, considering number of variations i've tried, i'd have seen @ to the lowest degree couple of errors indicate middleware indeed beingness triggered. unluckily, can't seem set finger on i'm doing wrong.

please advise.

'remove' middleware runs when remove instance method called (model#remove), not class method (model.remove). akin how 'save' middleware called on save not on update.

so you'd need rewrite deleteauthor method like:

authorschema.statics.deleteauthor = function(authorid, callback) { this.findbyid(authorid, function(err, author) { if (author) { author.remove(callback); } else { callback(err); } }); };

node.js mongodb express mongoose

No comments:

Post a Comment