Meteor.call 的基礎知識

Meteor.call(name, [arg1, arg2...], [asyncCallback])

(1)name String
(2)呼叫方法的名稱
(3)arg1,arg2 … EJSON-able 物件[可選]
(4)asyncCallback 函式[可選]

一方面,你可以這樣做:(通過 Session 變數,或通過 ReactiveVar

    var syncCall = Meteor.call("mymethod") // Sync call 

這意味著如果你做這樣的事情,伺服器端你會做:

    Meteor.methods({
        mymethod: function() {
            let asyncToSync =  Meteor.wrapAsync(asynchronousCall);
            // do something with the result;
            return  asyncToSync; 
        }
    });

另一方面,有時你會想通過回撥的結果保留它嗎?

客戶端 :

Meteor.call("mymethod", argumentObjectorString, function (error, result) {
    if (error) Session.set("result", error); 
    else Session.set("result",result);
}
Session.get("result") -> will contain the result or the error;

//Session variable come with a tracker that trigger whenever a new value is set to the session variable. \ same behavior using ReactiveVar

伺服器端

Meteor.methods({
    mymethod: function(ObjectorString) {
        if (true) {
            return true;
        } else {
            throw new Meteor.Error("TitleOfError", "ReasonAndMessageOfError"); // This will and up in the error parameter of the Meteor.call
        }
    }
});

這裡的目的是表明 Meteor 提出了各種方法來在客戶端和伺服器之間進行通訊。