Meteor 我如何知道一个方法是被称为服务器端还是客户端(从服务器端)?

Meteor 我如何知道一个方法是被称为服务器端还是客户端(从服务器端)?,meteor,Meteor,在我的Meteor应用程序中,我在服务器上定义了一个方法,如下所示: /* global Meteor */ Meteor.methods({ notifyRequestRejected: function (username, reason) { if (!Meteor.userId()) { throw new Meteor.Error(403, 'Access denied'); } return Meteor

在我的Meteor应用程序中,我在服务器上定义了一个方法,如下所示:

/* global Meteor */
Meteor.methods({
    notifyRequestRejected: function (username, reason) {
        if (!Meteor.userId()) {
            throw new Meteor.Error(403, 'Access denied');
        }
        return Meteor.http.post('…');
    }
});

由于我只希望经过身份验证的用户和我的服务器端(受信任)代码能够调用该方法,因此如何检查该方法是调用服务器端还是客户端?方法是否有类似于从服务器调用this.calledFromServer的内容?

可以通过检查this.connection属性来检查方法调用是在服务器端还是客户端进行的

如果不为null,则表示该方法是从客户端调用的

因此,要确保调用方是经过身份验证的用户或某些服务器端代码,请使用:

Meteor.methods({
    notifyRequestRejected: function (username, reason) {
        if (!Meteor.userId() && this.connection) {
            throw new Meteor.Error(403, 'Access denied');
        }
        // etc.
    }
});

您可以使用
this.isSimulation
Meteor.isClient
来检测客户端存根的执行


您可以否定这些表达式以检测服务器端环境,或者使用
Meteor.isServer

Downvoter,需要解释吗?