Javascript 为什么';调用此服务器方法在Meteor中是否有效?
在Meteor项目的服务器/服务器目录中有一个名为authServices.coffee的文件。它具有以下函数定义,后跟一个Meteor.methods调用: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
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
是的,这就是我在实现方面的实际结果。我正在寻找一个关于这个
及其行为的很好的解释。请看。我不需要它们……但它们看起来更好。