2013-03-13 43 views
1

在我的用户采集渲染数组列表,我有叫synonym_ids的数组。一个储存在一个Collection流星

什么是显示这个客户端上的最简单的方法?

我试图从发布服务器下面,然后从客户端订阅。不过,我得到这个错误:

Internal exception while starting subscription 0ea473b6-8be4-43ec-8a56-988409a4b58a Error: Meteor.userId can only be invoked in method calls. Use this.userId in publish functions.

#server 
Meteor.publish 'synonym_ids',() -> 
    if Meteor.userId() 
     return Meteor.users.findOne({_id: Meteor.userId()}).synonym_ids 


#client 
Meteor.autosubscribe() -> 
    Meteor.subscribe 'synonym_ids' 
+2

您是否尝试过repla通过this.UserId ceMeteor.userId()? – 2013-03-13 01:46:06

+0

@Oscar,那么我将如何将其表现为模板? – Gezim 2013-03-13 02:06:11

+0

@Oscar是对的:将'if Meteor.userId()'改为'if this.userId'。我想你可能还需要从publsh函数返回一个光标或者做更高级的事情。尝试'返回Meteor.users.find({_ id:this.userId},{fields:{synonym_ids:1}})'以确保用户看到该字段。然后在客户端上,一旦订阅完成,您应该可以使用'Meteor.user()。synonym_ids'来访问'synonym_ids'。 – zorlak 2013-03-13 06:35:10

回答

1

一个发布功能can't use Meteor.userId(),但它可以使用

#server 
Meteor.publish 'synonym_ids',() -> 
if this.userId() 
    return Meteor.users.findOne({_id: this.userId()}).synonym_ids 

在模板助手,一定要检查用户是否登录:

#client 
Template.home.synonym_ids = -> 
    Meteor.user().synonym_ids if Meteor.userId 
相关问题