Meteor Tutorial
Meteor Useful Resources
Selected Reading
- Meteor - Best Practices
- Meteor - ToDo App
- Meteor - Running on mobile
- Meteor - Deployment
- Meteor - Structure
- Meteor - Publish & Subscribe
- Meteor - Package.js
- Meteor - Methods
- Meteor - Accounts
- Meteor - Sorting
- Meteor - Security
- Meteor - Assets
- Meteor - Email
- Meteor - HTTP
- Meteor - EJSON
- Meteor - Timers
- Meteor - Blaze
- Meteor - Check
- Meteor - Core API
- Meteor - Packages
- Meteor - Tracker
- Meteor - Session
- Meteor - Events
- Meteor - Forms
- Meteor - Collections
- Meteor - Templates
- Meteor - First Application
- Meteor - Environment Setup
- Meteor - Overview
- Meteor - Home
Meteor Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Meteor - Methods
Meteor - Methods
Meteor methods are functions that are written on the server side, but can be called from the cpent side.
On the server side, we will create two simple methods. The first one will add 5 to our argument, while the second one will add 10.
Using Methods
meteorApp.js
if(Meteor.isServer) { Meteor.methods({ method1: function (arg) { var result = arg + 5; return result; }, method2: function (arg) { var result = arg + 10; return result; } }); } if(Meteor.isCpent) { var aaa = aaa Meteor.call( method1 , aaa, function (error, result) { if (error) { console.log(error); else { console.log( Method 1 result is: + result); } } ); Meteor.call( method2 , 5, function (error, result) { if (error) { console.log(error); } else { console.log( Method 2 result is: + result); } }); }
Once we start the app, we will see the calculated values in console.
![Meteor Methods Log](/meteor/images/meteor-methods-log.jpg)
Handpng Errors
For handpng errors, you can use the Meteor.Error method. The following example shows how to handle error for users that aren t logged in.
if(Meteor.isServer) { Meteor.methods({ method1: function (param) { if (! this.userId) { throw new Meteor.Error("logged-out", "The user must be logged in to post a comment."); } return result; } }); } if(Meteor.isCpent) { Meteor.call( method1 , 1, function (error, result) { if (error && error.error === "logged-out") { console.log("errorMessage:", "Please log in to post a comment."); } else { console.log( Method 1 result is: + result); }}); }
The console will show our customized error message.
![Meteor Methods Error](/meteor/images/meteor-methods-error.jpg)