Does it work with Meteor 1.3 or 1.4 with React?
Opened this issue · 4 comments
My experiment with Meteor 1.4 with React doesn't seem to work. Here's a demo of relevant code (unrelated code in ellipsis):
On the server side, for publishing:
...
import { Comments } from '../imports/collections/comments';
...
Meteor.publish("comments", function(){
Comments.join("Users", "user_id", "user");
return {
comments: Comments.find({"account_id": accountId});
}
});
On the client side, component CommentList:
...
import { Comments } from '../../collections/comments';
...
export default createContainer((props) => {
...
Comments.join("Users", "user_id", "user");
return {
comments: Comments.find({}).fetch() || {}
}
}, CommentList);
What happens is that I'm getting only the data in comments, nothing from users. So it seems that the join did not happen.
Is it not working, or am I using it wrong?
Thanks!
@maitrid you need to define join in single place visible both to client and to server. For example in /lib/ directory. It works with meteor 1.4 and reaact (I am using it in some projects).
Ah yes... you need to reference collection variable, not collection name string in the join (that doesn't work with 1.4), so:
(pseudo code)
import Users from "meteor-user-roles" (if are you using that package which exposes "Users" collection)
import Comments from ....
Comments.join(Users, "user_id", ....
One more thing: be careful with this: you need to pass one more argument: array of fields which will be fetched from users collection. If you not specify list of fields, all user fields will be exposed (entire user document).
And important: you need to publish data by returning: return Comments.publishJoinedCursors(comments)
(see README.md)
Thank you, everyone for the help! I made the join work, and found that actually it gets the records but it's an array, not a cursor, so the join cannot be used for publishing. I guess a good place to use is Meteor.methods...