我有以下代码:
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
处理程序?
答案 0 :(得分:0)
您要将console.log("ready")
的结果分配给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")