【问题标题】:Not all Mongo fields showing up when called in Meteor在 Meteor 中调用时,并非所有 Mongo 字段都会显示
【发布时间】:2016-02-26 11:31:37
【问题描述】:

当我 console.log 当前从服务器端登录的用户时,我会看到所有字段。当我从客户端做同样的事情时,我只会得到 _id 和电子邮件字段。我很确定我订阅正确。

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>
  );
 }
});

【问题讨论】:

    标签: mongodb meteor


    【解决方案1】:

    如果自动发布被移除,Meteor.users 只会发布当前用户的个人资料。

    将以下发布添加到您的服务器代码:

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

    如果删除自动发布,客户端将自动发布和自动订阅空发布。

    另外,请记住只包含那些不敏感的字段。对于,例如。您可能希望省略密码字段或服务字段。

    类似这样的:

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

    【讨论】:

      【解决方案2】:

      因为

      默认情况下,当前用户的用户名、电子邮件和个人资料是 发布给客户端。

      来自文档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;
      });
      

      【讨论】:

      • 如何读写用户的subreddit属性?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      相关资源
      最近更新 更多