Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

29 lines (26 sloc) 5.98 kb
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>Mongoose Middleware v3.3.1</title><link href="http://fonts.googleapis.com/css?family=Anonymous+Pro:400,700|Droid+Sans+Mono|Open+Sans:400,700|Linden+Hill|Quattrocento:400,700|News+Cycle:400,700|Antic+Slab|Cabin+Condensed:400,700" rel="stylesheet" type="text/css"><link href="/docs/css/default.css" rel="stylesheet" type="text/css"><link href="/docs/css/guide.css" rel="stylesheet" type="text/css"></head><body><a id="forkbanner" href="http://github.com/learnboost/mongoose"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png" alt="Fork me on GitHub"></a><div id="links"><div id="header"><h1><a href="../index.html"><div class="mongoose">Mongoose</div></a></h1></div><ul><li class="home"><a href="../index.html">home</a></li><li class="faq"><a href="./faq.html">FAQ</a></li><li class="plugins"><a href="http://plugins.mongoosejs.com">plugins</a></li><li class="changelog"><a href="http://github.com/learnboost/mongoose/tree/master/History.md">change log</a></li><li class="support"><a href="../index.html#support">support</a></li><li class="fork"><a href="http://github.com/learnboost/mongoose">fork</a></li><li class="guide"><a href="./guide.html">guide</a><ul><li class="double"><a href="./guide.html">schemas</a><ul><li class="schematypes"><a href="./schematypes.html"><span>schema</span>types</a></li></ul></li><li><a href="./models.html">models</a></li><li class="double"><a href="./documents.html">documents</a><ul><li class="subdocs"><a href="./subdocs.html">sub docs</a></li></ul></li><li><a href="./queries.html">queries</a></li><li><a href="./validation.html">validation</a></li><li><a href="./middleware.html">middleware</a></li><li><a href="./populate.html">population</a></li><li><a href="./connections.html">connections</a></li><li><a href="./plugins.html">plugins</a></li><li><a href="https://github.com/LearnBoost/mongoose/blob/master/CONTRIBUTING.md">contributing</a></li><li><a href="./migration.html">migrating from 2.x</a></li></ul></li><li class="api"><a href="./api.html">API docs</a></li><li class="quickstart"><a href="./index.html">quick start</a></li><li class="contrib"><a href="http://github.com/learnboost/mongoose/contributors">contributors</a></li><li class="prior"><a href="./prior.html">prior releases</a></li></ul></div><div id="content"><div class="module"><h2>Middleware</h2><p>Middleware are functions which are passed control of flow during execution of <a href="./api.html#document_Document-init">init</a>, <a href="./api.html#document_Document-validate">validate</a>, <a href="./api.html#model_Model-save">save</a> and <a href="./api.html#model_Model-remove">remove</a> methods.</p><p>
There are two types of middleware, serial and parallel.</p><h4>Serial</h4><p>Serial middleware are executed one after another, when each middleware calls <code>next</code></p><pre><code class="javascript"><span class="keyword">var</span> schema = <span class="keyword">new</span> Schema(..);
schema.pre(<span class="string">'save'</span>, <span class="function"><span class="keyword">function</span> <span class="params">(next)</span> {</span>
<span class="comment">// do stuff</span>
next();
});</code></pre><h4>Parallel</h4><p>Parallel middleware offer more fine-grained flow control.</p><pre><code class="javascript"><span class="keyword">var</span> schema = <span class="keyword">new</span> Schema(..);
schema.pre(<span class="string">'save'</span>, <span class="literal">true</span>, <span class="function"><span class="keyword">function</span> <span class="params">(next, done)</span> {</span>
<span class="comment">// calling next kicks off the next middleware in parallel</span>
next();
doAsync(done);
});</code></pre><p>The hooked method, in this case <code>save</code>, will not be executed until <code>done</code> is called by each middleware.</p><h4>Use Cases</h4><p>Middleware are useful for atomizing model logic and avoiding nested blocks of async code. Here are some other ideas:<ul><li>complex validation</li><li>removing dependent documents<ul><li>(removing a user removes all his blogposts)</li></ul></li><li>asynchronous defaults</li><li>asynchronous tasks that a certain action triggers<ul><li>triggering custom events</li><li>notifications</li></ul></li></ul></p><h4>Error handling</h4><p>If any middleware calls <code>next</code> or <code>done</code> with an <code>Error</code> instance, the flow is interrupted, and the error is passed to the callback.</p><pre><code class="javascript">schema.pre(<span class="string">'save'</span>, <span class="function"><span class="keyword">function</span> <span class="params">(next)</span> {</span>
<span class="keyword">var</span> err = <span class="keyword">new</span> Error(<span class="string">'something went wrong'</span>);
next(err);
});
<span class="comment">// later...</span>
myModel.save(<span class="function"><span class="keyword">function</span> <span class="params">(err)</span> {</span>
console.log(err.message) <span class="comment">// something went wrong</span>
});
</code></pre><h3 id="next">Next Up</h3><p>Now that we&#39;ve covered <code>middleware</code>, let&#39;s take a look at Mongooses approach to faking JOINs with its query <a href="/docs/populate.html">population</a> helper.</p></div></div><script>document.body.className = 'load';</script><script>var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1122274-9']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();</script></body></html>
Jump to Line
Something went wrong with that request. Please try again.