在mongoos pre/save /(串行/并行)中间件中绑定了哪些函数/下一个函数

GJa*_*ain 6 mongoose node.js

尝试通过docs/blogs(Tim Casewell)了解mongoose中间件(pre/save/parallel).

基于http://mongoosejs.com/docs/middleware.html

var schema = new Schema(..);
schema.pre('save', true, function (next, done) {
  // calling next kicks off the next middleware in parallel
  next();
  doAsync(done);
});

The hooked method, in this case save, will not be executed until done is called by each middleware.
Run Code Online (Sandbox Code Playgroud)

这里做了什么/接下来要绑定什么?你能举一个如何使用它的完整例子吗?

例如:我使用serial如下:

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();
  }
});
Run Code Online (Sandbox Code Playgroud)

接下来要绑定什么?它需要绑定到要执行的东西?我不明白next/done指的是哪个函数?

如果您可以为上面的代码详细说明控制流程,那将是一个很大的帮助.

------------- 这只是为了阐述我的理解(不是问题的一部分) ------

   * On executing myModel.save(...)
   * Control Flow will be passed to pre/save
   * if self.validateSomething() fails,
     * Document will not be tried to be saved in DB
     * "Error Occurred" will be printed on console
   * if validation succeeds,
     * Control Flow should be passed to *somewhere* in Mongoose libs 
       * Document will be tried to save in DB
         * On Success, "Document saved" will be printed on the console
         * On Failure, "Error Occurred" will be printed on console
Run Code Online (Sandbox Code Playgroud)

wpr*_*prl 8

它们与在Mongoose内部提供流量控制的功能绑定在一起.Mongoose依赖于hooks-js来获得其中间件功能,有关更多详细信息,请参阅源代码.

例如,如果您有多个预保存中间件函数,next将调用将调用下一个预保存中间件的函数或出错,将错误传递回您的save回调.

使用done是一个更高级的流控制选项,允许多个预先保存,例如,中间件一次执行,但不会超过预保存步骤,直到done在所有中间件功能中调用.