0

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

我使用 Meteor、Flow-Router、React。我想订阅追随者,但是当我运行应用程序时它总是返回空。我不确定出了什么问题。

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

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

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代码在这里:

库/路由器

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

4

1 回答 1

0

发布函数应该返回光标:

Meteor.publish('followers', (userId) => {
  check(userId, String);

  return Meteor.users.find({followings: userId});
});
于 2015-12-11T13:45:50.833 回答