node.js - which functions done/next are bound to in mongoos pre/save/(serial/parallel) middleware -
trying understand mongoose middleware (pre/save/parallel) thru docs/blogs(tim casewell).
based on http://mongoosejs.com/docs/middleware.html
var schema = new schema(..); schema.pre('save', true, function (next, done) { // calling next kicks off next middleware in parallel next(); doasync(done); }); hooked method, in case save, not executed until done called each middleware.
what done/next bound here? can please give complete example of how use it?
for eg: use serial follows:
mymodel.save(function(err) { if (err) console.error("error occured") else console.info("document stored"); }); schema.pre('save', function(next) { if (!self.validatesomething()) { next(new error()); } else { next(); } });
what next bound here? needs bound executed? fail understand function(s) next/done referring to?
if elaborate control flow code above, great help.
-------------this elaborate understanding (not part of question)------
* on executing mymodel.save(...) * control flow passed pre/save * if self.validatesomething() fails, * document not tried saved in db * "error occurred" printed on console * if validation succeeds, * control flow should passed *somewhere* in mongoose libs * document tried save in db * on success, "document saved" printed on console * on failure, "error occurred" printed on console
they're bound functions provide flow control internally within mongoose. mongoose depends on hooks-js middleware functionality, see source more details.
for example, if have multiple pre-save middleware functions, next
call function call next pre-save middleware or on error, pass error save
callback.
using done
more advanced option flow control, allowing multiple pre-save, example, middleware execute @ once, not moving past pre-save step until done
has been called in middleware functions.
Comments
Post a Comment