Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
MEAN Web Development

You're reading from   MEAN Web Development Master real-time MEAN web application development and learn how to construct a MEAN application using a combination of MongoDB, Express, AngularJS, and Node.js

Arrow left icon
Product type Paperback
Published in Sep 2014
Publisher Packt
ISBN-13 9781783983285
Length 354 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Amos Q. Haviv Amos Q. Haviv
Author Profile Icon Amos Q. Haviv
Amos Q. Haviv
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Introduction to MEAN 2. Getting Started with Node.js FREE CHAPTER 3. Building an Express Web Application 4. Introduction to MongoDB 5. Introduction to Mongoose 6. Managing User Authentication Using Passport 7. Introduction to AngularJS 8. Creating a MEAN CRUD Module 9. Adding Real-time Functionality Using Socket.io 10. Testing MEAN Applications 11. Automating and Debugging MEAN Applications Index

Defining custom model methods


Mongoose models are pretty packed with both static and instance predefined methods, some of which you already used before. However, Mongoose also lets you define your own custom methods to empower your models, giving you a modular tool to separate your application logic properly. Let's go over the proper way of defining these methods.

Defining custom static methods

Model static methods give you the liberty to perform model-level operations, such as adding extra find methods. For instance, let's say you want to search users by their username. You could of course define this method in your controller, but that wouldn't be the right place for it. What you're looking for is a static model method. To add a static method, you will need to declare it as a member of your schema's statics property. In our case, adding a findOneByUsername() method would look like the following code snippet:

UserSchema.statics.findOneByUsername = function (username, callback) {
  this.findOne...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime