无法使用Meteor中的Flow-Router / React在React Component内订阅关注者

时间:2015-12-11 12:12:36

标签: mongodb meteor reactjs flow-router

问题: 我无法使用Flow-Router + React获取用户的关注者,在React Component中订阅

我使用Meteor,Flow-Router,React。我想订阅粉丝, 但是当我运行应用程序时它总是返回空。我不确定有什么问题。

我遵循这种方法:通过Arunoda在React Component内部订阅

当我使用mongo shell检查查询时,它可以工作: (即返回Alice的追随者(1位用户,Bob),其_id是“5MJJPc78W3ipXpWzy”)

meteor:PRIMARY> db.users.find({followings: "5MJJPc78W3ipXpWzy"}).pretty()
{
  "_id" : "v2Kikp8Wa5FSJi64b",
  "createdAt" : ISODate("2015-12-11T11:08:50.209Z"),
  "services" : {
    "password" : {
      "bcrypt" : "$2a$10$IzfXjbXlYw4BuTMLroSjaOmgqnj8Z9sWXc4uyvHuXurirWRgDcZJ2"
    },
    "resume" : {
      "loginTokens" : [
        {
          "when" : ISODate("2015-12-11T11:08:50.213Z"),
          "hashedToken" : "jN0jeZX6PYFAy6b1eHvwWvbMhiVtbF1cjFySPnTpQTQ="
        }
      ]
    }
  },
  "username" : "bob",
  "followings" : [
    "5MJJPc78W3ipXpWzy"
  ]
}

y代码在这里:

LIB /路由器

FlowRouter.route('/users/:userid/followers', {
  name: 'followers',
  action(params) {
    ReactLayout.render(MainLayout, {
      content: <FollowersBox {...params}/>
    });
  }
});

服务器/出版物

Meteor.publish('followers', (userId)=> {
  check(userId, String);
  Meteor.users.find({followings: userId});
});

客户端/组件/资料/ FollowersBox

FollowersBox = React.createClass({
  mixins: [ReactMeteorData],

  getMeteorData() {
    let data = {};
    let followersSubs = Meteor.subscribe('followers', this.props.userid); // Always empty!

    if(followersSubs.ready()) { // Never be ready
      data.followers = Meteor.users.find({followings: this.props.userid}).fetch();
    }
    return data;
  },

  render(){
    let followersList = "";
    if(this.data.followers){
      followersList = this.data.followers.map((user)=> {
        return <UserItem
              key={user._id}
              userId={user._id}
              username={user.username}
              />
      });
    } 

    return (
      <div>
        <h4>Followers</h4>
        <ul>
          {followersList}
        </ul>
      </div>
    )
  }
});

完整回购 https://github.com/yhagio/MeteorReactTwitter

1 个答案:

答案 0 :(得分:0)

发布功能应该返回光标:

$users