Meteor.call(name, [arg1, arg2...], [asyncCallback])
(1) name String
(2) 要调用的方法名称
(3) arg1, arg2... EJSON-able Object [Optional]
(4) asyncCallback Function [Optional]
一方面,您可以这样做:(通过Session variable,或通过ReactiveVar)
var syncCall = Meteor.call("mymethod") // 同步通话
这意味着如果你做这样的事情,服务器端你会做:
Meteor.methods({ mymethod: function() { let asyncToSync = Meteor.wrapAsync(asynchronousCall); // 用结果做点什么; 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; //会话变量带有一个跟踪器,每当会话变量设置新值时就会触发。\ 使用 ReactiveVar 的相同行为
服务器端
Meteor.methods({ mymethod: function(ObjectorString) { if (true) { return true; } else { throw new Meteor.Error("TitleOfError", "ReasonAndMessageOfError"); // 这将出现在 Meteor.call 的错误参数中 } } });
这里的目的是展示 Meteor 提出了多种方式来在客户端和服务器之间进行通信。