perak / meteor-joins

Collection joins for Meteor
77 stars 7 forks source link

Does it work with Meteor 1.3 or 1.4 with React? #11

Open maitrid opened 8 years ago

maitrid commented 8 years ago

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!

perak commented 8 years ago

@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).

perak commented 8 years ago

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", ....
perak commented 8 years ago

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)

maitrid commented 8 years ago

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...