I’ve been trying for the last couple of hours to make the following work to no avail.
In brief :
When the component loads I’m getting the value of a url parameter, using that to query a collection, set a value based on the returned document and then trying to pass that value into another prop which in turns makes a call to a different collection setting a second set of data to a separate prop.
I guess you could say I’m trying a very hacky way of faking a join with Mongo.
This is the export so you can see what I’m attempting but it’s not working, undefined always gets returned :
export default withTracker(props => {
const profileHandle = Meteor.subscribe('usermeta');
const profile = UserMeta.find({username: props.match.params.username}).fetch();
const myFollowersHandle = Meteor.subscribe('followers');
return {
profileHandleReady: profileHandle.ready(),
profile: UserMeta.find({username: props.match.params.username}).fetch(),
myFollowersReady: myFollowersHandle.ready(),
myFollowers: FollowersCollection.find({uid: props.profile.uid}).fetch()
};
})(FollowersList);
The last call is what’s giving me problems :
myFollowers: FollowersCollection.find({uid: props.profile.uid}).fetch()
I’ve also tried it without using props, same result :
myFollowers: FollowersCollection.find({uid: profile.uid}).fetch()
I’ve come to the conclusion that it just isn’t supposed to work this way but everything else I’ve tried, setting state, running code in componentDidMount() and even trying conditional render statements all give the same result. Either props.profile.uid is undefined or an empty array.
If I console log it on componentDidUpdate() I can see the value after a couple of undefined ones first, but I just can’t get the data back before the final Collection call is made.
I thought .ready was supposed to address this but it doesn’t seem to be able to help me here.
Is there anyway of forcing the following to return completed before doing anything else?
- Subscribe to this collection -> Meteor.subscribe(‘usermeta’);
- Get the object -> const profile = UserMeta.findOne({username: props.match.params.username});
- Get the desired value from the object -> profile[0].uid or profile.uid (depending on how it comes back)
ThenI can finally pass the desired value into the second collection call inside withTracker -> myFollowers: FollowersCollection.find({uid: .profile[0].uid}).fetch()
Or is there a better way of achieving what I’m trying to do?