2016-02-25 30 views
0

當我console.log當前從服務器端登錄的用戶時,我看到所有字段。當我從客戶端執行同樣的操作時,我只能看到_id和email字段。我很確定我訂閱正確。並非所有在流星中調用的Mongo字段

Accounts.onCreateUser(function(options, user){ 
    user.subreddits = []; 
    console.log(user); 
    return user; 
}); 

Meteor.publish('users', function(){ 
    return Meteor.users.find().fetch(); 
}); 

SinglePost = React.createClass({ 
    getInitialState: function(){ 
    Meteor.subscribe('users'); 
    return { 
    }; 
    }, 
deletePost: function(){ 
    var post_to_delete = {_id: this.props.id} 
    Posts.remove(post_to_delete); 
}, 
showMeInfo: function(){ 
    console.log(Meteor.user()); 
}, 
render: function(){ 
    return (
    <div> 
     <li> 
     <button onClick={this.deletePost}> Click Me!</button>    {this.props.title}: {this.props.content} 
     </li> 
    <p><button onClick={this.showMeInfo}> Show Me Info! </button>   </p> 
    </div> 
); 
} 
}); 

回答

0

正是由於

默認情況下,當前用戶的用戶名,電子郵件和個人資料被公佈 到客戶端。

從DOC here

更新您的onCreateUser這樣

Accounts.onCreateUser(function(options, user){ 

    if (options.profile) user.profile = options.profile; 
    if (user.profile == null) user.profile = {}; 

    user['profile'].subreddits = []; 
    console.log(user); 
    return user; 
}); 
+0

我如何能夠讀取和寫入用戶的屬性版(Subreddit)? – ChrisWilson

+0

你必須把它放在用戶['profile'] –

+0

更新的答案用於保存'user ['profile']' –

0

Meteor.users纔會公佈,如果是自動發佈刪除當前用戶的配置文件。

添加下面的發佈到你的服務器代碼:

Meteor.publish(null, function() { 
    if (!this.userId) return this.ready(); 
    return Meteor.users.find({}); 
}); 

空發佈,如果被自動發佈除去將自動發佈和自動訂閱客戶端。

此外,請記住只包含那些不敏感的字段。例如,您可能想要省略密碼字段或服務字段。

事情是這樣的:

Meteor.users.find({}, { 
     fields: { 
      profile : 1, 
      emails : 1 
     } 
    });