无法在 Meteor 中使用 Flow-Router/React 订阅 React Component 内的关注者
Cannot subscribe followers inside React Component with Flow-Router/React in Meteor
问题:
我无法使用 Flow-Router + React 获得用户的关注者,在 React Component
内订阅
我使用 Meteor、Flow-Router、React。我正在尝试订阅关注者,
但是当我 运行 应用程序时它总是 returns 空的。我不确定哪里出了问题。
我遵循这种方法:通过 A运行oda
在 React Component 内订阅
当我用 mongo shell 检查查询时,它有效:
(即 Return _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" : "a$IzfXjbXlYw4BuTMLroSjaOmgqnj8Z9sWXc4uyvHuXurirWRgDcZJ2"
},
"resume" : {
"loginTokens" : [
{
"when" : ISODate("2015-12-11T11:08:50.213Z"),
"hashedToken" : "jN0jeZX6PYFAy6b1eHvwWvbMhiVtbF1cjFySPnTpQTQ="
}
]
}
},
"username" : "bob",
"followings" : [
"5MJJPc78W3ipXpWzy"
]
}
y 个代码在这里:
lib/router
FlowRouter.route('/users/:userid/followers', {
name: 'followers',
action(params) {
ReactLayout.render(MainLayout, {
content: <FollowersBox {...params}/>
});
}
});
server/publications
Meteor.publish('followers', (userId)=> {
check(userId, String);
Meteor.users.find({followings: userId});
});
clients/components/Profile/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>
)
}
});
发布函数应该return光标:
Meteor.publish('followers', (userId) => {
check(userId, String);
return Meteor.users.find({followings: userId});
});
问题: 我无法使用 Flow-Router + React 获得用户的关注者,在 React Component
内订阅我使用 Meteor、Flow-Router、React。我正在尝试订阅关注者, 但是当我 运行 应用程序时它总是 returns 空的。我不确定哪里出了问题。
我遵循这种方法:通过 A运行oda
在 React Component 内订阅当我用 mongo shell 检查查询时,它有效: (即 Return _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" : "a$IzfXjbXlYw4BuTMLroSjaOmgqnj8Z9sWXc4uyvHuXurirWRgDcZJ2"
},
"resume" : {
"loginTokens" : [
{
"when" : ISODate("2015-12-11T11:08:50.213Z"),
"hashedToken" : "jN0jeZX6PYFAy6b1eHvwWvbMhiVtbF1cjFySPnTpQTQ="
}
]
}
},
"username" : "bob",
"followings" : [
"5MJJPc78W3ipXpWzy"
]
}
y 个代码在这里:
lib/router
FlowRouter.route('/users/:userid/followers', {
name: 'followers',
action(params) {
ReactLayout.render(MainLayout, {
content: <FollowersBox {...params}/>
});
}
});
server/publications
Meteor.publish('followers', (userId)=> {
check(userId, String);
Meteor.users.find({followings: userId});
});
clients/components/Profile/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>
)
}
});
发布函数应该return光标:
Meteor.publish('followers', (userId) => {
check(userId, String);
return Meteor.users.find({followings: userId});
});