Javascript 为什么';调用此服务器方法在Meteor中是否有效?

Javascript 为什么';调用此服务器方法在Meteor中是否有效?,javascript,coffeescript,meteor,Javascript,Coffeescript,Meteor,在Meteor项目的服务器/服务器目录中有一个名为authServices.coffee的文件。它具有以下函数定义,后跟一个Meteor.methods调用: isAuthorized = () -> # note that at this point there is ever to be but one authorized user authorized = Assets.getText('authorizedUsers').trim() this.user

在Meteor项目的服务器/服务器目录中有一个名为authServices.coffee的文件。它具有以下函数定义,后跟一个Meteor.methods调用:

 isAuthorized = () ->
    # note that at this point there is ever to be but one authorized user
    authorized = Assets.getText('authorizedUsers').trim()
    this.userId.trim() is authorized
这不起作用——也就是说,调用
Meteor.call'getKey'
返回
undefined

Meteor.methods(
    isAuthorized : isAuthorized
    getKey : () -> 
    if isAuthorized()
        Assets.getText('Key')
)
但是如果我在上面的
getKey
中内联
isAuthorized
,它将返回
true
(给定正确的输入)


我猜这是一个函数,它决定了这些对象的
行为,但不能完全控制它。

这是一个选项吗

isAuthorized = (userId) ->
    # note that at this point there is ever to be but one authorized user
    authorized = Assets.getText('authorizedUsers').trim()
    userId.trim() is authorized

Meteor.methods(
    isAuthorized : () -> 
        isAuthorized(this.userId)
    getKey : () -> 
        if isAuthorized(this.userId)
            Assets.getText('Key')
)

函数
已授权
,方法
getKey
具有不同的上下文。最简单的解决方案就是实现
getKey
,如下所示:

getKey: ->
  if Meteor.call 'isAuthorized'
    Assets.getText 'Key'
或者,您可以手动使用
调用
应用
来传递
,或者您可以将
@userId
作为参数传递给
已授权的
函数


样式点:当使用CS时,当函数不带参数时,您不需要空参数。

除了发布函数和方法之外,您不需要使用Meteor.userId而不是this.userId吗?在
中将this
更改为
Meteor.userId
中的
authorizedUsers
抛出新错误(“Meteor.userId只能在方法调用中调用。在发布函数中使用this.userId。”);/17
是的,这就是我在实现方面的实际结果。我正在寻找一个关于
这个
及其行为的很好的解释。请看。我不需要它们……但它们看起来更好。