中间件是一些函数, 当document发生init, validate, save和remove方法的时候中间件发生.

中间件都是document级别的不是model级别的.

下面讲讲两种中间件pre和post

pre

一共有两种pre中间件serial和parallel 发生在被附挂方法之前

serial

serial中间件是一个一个执行的串行执行

var schema = new Schema(..);
schema.pre('save', function(next){
//do stuff
next();
});

parallel

并行执行

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

这个例子中被hooked的方法是save, 直到所有的中间件都done了save才会真正被执行

错误处理

如果中间件调用一个包含error的next, 流程会被终止, 错误会被发送到callback

schema.pre('save', function(next){
var err = new Error('something went wrong');
next(err);
}); //later... myDoc.save(function(err){
console.log(err.message) // something went wrong
});

  

post

发生在被附挂的方法之后. post中间件不直接参与到整个流程, 所以他的callback没有next也没有done

schema.post('init', function (doc) {
console.log('%s has been initialized from the db', doc._id);
})
schema.post('validate', function (doc) {
console.log('%s has been validated (but not saved yet)', doc._id);
})
schema.post('save', function (doc) {
console.log('%s has been saved', doc._id);
})
schema.post('remove', function (doc) {
console.log('%s has been removed', doc._id);
})

注意了!!!

如果更新操作直接发生在数据库上pre和post不会被调用, 包含Model.update,  findByIdAndUpdate, findOneAndUpdate, findOneAndRemove, findByIdAndRemove

[译]Mongoose指南 - 中间件的更多相关文章

  1. [译]Mongoose指南 - 验证

    开始前记住下面几点 Validation定义在SchemaType中 Validation是一个内部的中间件 当document要save前会发生验证 验证不会发生在空值上 除非对应的字段加上了 re ...

  2. [译]Mongoose指南 - Schema

    定义schema 用mongoose的第一件事情就应该是定义schema. schema是什么呢? 它类似于关系数据库的表结构. var mongoose = require('mongoose'); ...

  3. [译]Mongoose指南 - Population

    MongoDB没有join, 但是有的时候我们需要引用其它collection的documents, 这个时候就需要populate了. 我们可以populate单个document, 多个docum ...

  4. [译]Mongoose指南 - Connection

    使用mongoose.connect()方法创建连接 mongoose.conect('mongodb://localhost/myapp'); 上面的代码是通过默认端口27017链接到mongodb ...

  5. [译]Mongoose指南 - 查询

    查询有带callback和不带callback两种方式 所有mongoose的callback都是这种格式: callback(err, result) var Person = mongoose.m ...

  6. [译]Mongoose指南 - Model

    编译你的第一个model var xxSchema = new Schema({name: 'string', size: 'string'}); var Tank = mongoose.model( ...

  7. [译]Mongoose指南 - Plugin

    Schema支持插件, 这样你就可以扩展一些额功能了 下面的例子是当document save的时候自定更新最后修改日期的出插件 // lastMod.js module.exports = expo ...

  8. [译]Mongoose指南 - Document

    更新 有几种方式更新document. 先看一下传统的更新方法 Tank.findById(id, function(err, tank){ if(err) return handleError(er ...

  9. (译)快速指南:用UIViewPropertyAnimator做动画

    翻译自:QUICK GUIDE: ANIMATIONS WITH UIVIEWPROPERTYANIMATOR 译者:Haley_Wong iOS 10 带来了一大票有意思的新特性,像 UIViewP ...

随机推荐

  1. hdu5175 gcd 求约数

    题意:求满足条件GCD(N,M) = N XOR M的M的个数 sol:和uva那题挺像的.若gcd(a,b)=a xor b=c,则b=a-c 暴力枚举N的所有约数K,令M=NxorK,再判断gcd ...

  2. Bzoj2756 [SCOI2012]奇怪的游戏

    2756: [SCOI2012]奇怪的游戏 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 3220  Solved: 886 Description ...

  3. dedecms /member/uploads_edit.php SQL Injection Vul

    catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 Dedecms 5.3版本下的member/uploads_edit.p ...

  4. Linux、Windows Server Password Security Policy Strengthen

    catalog . windows Security and Protection(Logon and Authentication) . windows密码强制安全策略 . PAM(Pluggabl ...

  5. lua的corroutine学习

    lua的corroutine学习 function receive (prod) local status, value = coroutine.resume(prod) return value e ...

  6. Hong Kong Regional Online Preliminary 2016 C. Classrooms

    Classrooms 传送门 The new semester is about to begin, and finding classrooms for orientation activities ...

  7. R in bioinformatic

    TCGA https://www.bioconductor.org/packages/release/bioc/vignettes/TCGAbiolinks/inst/doc/tcgaBiolinks ...

  8. canvas练习

    <!doctype html><html><head><meta charset="utf-8"><title>move ...

  9. SQL Server 2012 学习笔记2

    1. 新建数据库 可以在对应目录下右键新建数据库,也可以用程序添加: 先打开程序编辑对话框"New Query" create database Library 2. 添加表格 可 ...

  10. 在C#中使用官方驱动操作MongoDB

    MongoDB的官方驱动下载地址:https://github.com/mongodb/mongo-csharp-driver/releases 目前最新的版本是2.10,支持.NET 4.5以上.由 ...