有没有办法可以从服务器向客户端发送警报?例如,用户单击按钮。该按钮调用服务器上的方法,该方法检查是否已为用户分配了ID#。如果没有为用户分配ID#,我希望浏览器收到警报。如果我将ID#发布到客户端,我可以很容易地检查,但ID#非常敏感,因此我不想发布它。有什么想法吗?提前谢谢。
答案 0 :(得分:2)
您可以尝试以下内容:
1)在客户端上,创建一个运行Meteor方法的按钮单击侦听器。
// CLIENT
Template.example.events({
'click button': function () {
Meteor.call('checkIfUserHasId', function (err, userHasId) {
if (!userHasId) {
alert('user has no id');
}
});
}
});
2)在服务器上,创建Meteor方法,检查用户是否具有id。
// SERVER
Meteor.methods({
checkIfUserHasId: function () {
// check if user has id
return true; // or false depending whether user has id or not
}
});
Meteor方法可以在客户端远程调用,但在服务器上定义。这有助于实现您在执行检查时不暴露id的所需要的内容。