0

我有以下代码:

GetPublication = new Meteor.Collection 'get-publication'

Meteor.autorun ->
  Meteor.subscribe 'get-publication', Session.get 'currentPublicationId', {
    onReady: console.log "ready"
    onError: (error) -> console.error "error", error
  }

Template.publication.publication = ->
  # How to know here what was an error thrown in subscription?
  JSON.stringify GetPublication.findOne()

我有一个自定义集合:

Meteor.publish 'get-publication', (publicationId) ->
  self = this
  self.ready()
  self.error new Meteor.Error 500, "Test"

我想在模板中输出带有订阅中抛出的内容的消息,而不是(空)发布集合结果。

此外,为什么不调用onReady和处理程序?onError

4

1 回答 1

0

您正在分配console.log("ready")to的结果onReady。正确的语法是:

onReady: -> console.log "ready"

如果你想打印你给出的错误,我想你想将从错误回调中获得的内容存储在一个变量(例如Session)中,然后在模板中打印它。像这样的东西:

Meteor.autorun ->
  Meteor.subscribe 'get-publication', Session.get 'currentPublicationId', {
    onReady: -> console.log "ready"
    onError: (error) -> Session.set "get-publication-error", error
  }

Template.publication.publicationError = ->
  JSON.stringify(Session.get "get-publication-error")
于 2013-03-19T23:06:14.430 回答