Meteor 服务器端方法在方法调用上抛出错误,但在客户端上没有

Meteor 服务器端方法在方法调用上抛出错误,但在客户端上没有,meteor,Meteor,在我的客户机上,我有以下代码 # If clicking on unit while target is set, activates action 'click .actor': () -> character = Meteor.user().character() target = Session.get('target') skill = Session.get('selectedSkill') if target and skill

在我的客户机上,我有以下代码

# If clicking on unit while target is set, activates action
  'click .actor': () ->
    character = Meteor.user().character()
    target = Session.get('target')
    skill = Session.get('selectedSkill')

    if target and skill
      console.log character.battle()
      Meteor.call('useSkill', skill, character, target, (err) ->
        if err then console.log err.reason
      )
在这里,当我调用
字符.battle()
时,它会正确地返回battle文档。但是当我在
useSkill()
方法中对同一对象调用相同的方法时,它抛出以下错误

Exception while invoking method 'useSkill' TypeError: Object #<Object> has no method 'battle'
以及关联“
battle()
”方法

@Characters = new Meteor.Collection('characters', 
    transform: (entry) ->

        entry.battle = () ->
            Battles.findOne({active: true, $or: [{characterOneId: this._id}, {characterTwoId: this._id}]})

        return entry
)

只能将可序列化对象作为方法参数传递。换句话说,当您像这样传递
actor
参数时,在客户端添加的所有自定义方法都不会被保留。要访问服务器上的自定义函数,需要在服务器上创建对象

因此,不要传递整个
字符
对象,只需传递其
\u id
并在方法中再次找到它:

useSkill: (skill, actorId, target) ->
  actor = Characters.findOne actorId
  ...
  console.log "battle: #{actor.battle()}"

函数不是由EJSON序列化的,也不是通过DDP发送的。 在
Meteor.call中
pass字符id:

Meteor.call('useSkill', skill, character._id, target, (err) ->
   if err then console.log err.reason
)
useSkill
方法中查找参与者:

useSkill: (skill, actorId, target) ->
    cost = skill.cost
    actor = Characters.findOne actorId
    console.log "battle: #{actor.battle()}"

你是在
服务器和
客户端上声明
字符集吗?我是在lib文件夹中声明的,所以它应该在两者上都可用。这是一种更好的方法,因为它可以节省带宽。除了向meteor服务器方法调用传递id之外,是否应该传递任何内容?嗯,通常不应该传递可以在服务器上获取的整个数据库项,在这种情况下,最好传递
\u id
。不过,通常你会传递其他东西,比如你想插入的整个对象(所以它还不在数据库中)。另外:你永远不能传递函数。问题是我必须传递不同的集合:角色和灵魂,它们基本上是相同的,我尝试通过添加一个转换方法“collection”来实现多态性,该方法返回集合,这样我就可以调用eval(actor.collection).find..,但当我只发送ID时,这是不可能的
useSkill: (skill, actorId, target) ->
    cost = skill.cost
    actor = Characters.findOne actorId
    console.log "battle: #{actor.battle()}"