Meteor - Metodi
I metodi Meteor sono funzioni scritte sul lato server, ma possono essere chiamate dal lato client.
Sul lato server, creeremo due semplici metodi. Il primo aggiungerà 5 al nostro argomento, mentre il secondo aggiungerà10.
Utilizzo di metodi
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.isClient) {
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);
}
});
}
Una volta avviata l'app, vedremo i valori calcolati nella console.

Gestione degli errori
Per la gestione degli errori, puoi utilizzare il file Meteor.Errormetodo. L'esempio seguente mostra come gestire l'errore per gli utenti che non hanno effettuato l'accesso.
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.isClient) { 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);
}});
}
La console mostrerà il nostro messaggio di errore personalizzato.
