How to get user doc by id using data from a post?


#1

Ok, I’m pretty confused about this. With Blaze this was easy… with React, not so much.

Let’s say we have a post, with a createdBy field. The createdBy field stores the _id of the user who created it.

With React, how can I get the user’s document with that createdBy id? This would be dynamic, for example - there might be 100 posts in a list.

Should it be method based, or container based? I’m definitely pretty confused about this.


#2

Bump
Up
My
Post

Bump


#3

Are you try to get data from the database? You would still have to use pub/sub. Sounds like you are looking for createContainer. So I guess that is what you meant by container based. Also there is an alternative react-komposer.

The meteor chef has articles on both.
https://themeteorchef.com/snippets/using-create-container/
https://themeteorchef.com/snippets/using-react-komposer/

You can also get data with methods if you would like, but that data will be non-reactive. Also there is the upcoming Apollo: http://dev.apollodata.com/core/meteor.html


#4

@townmulti Basically, with the smart/dumb component model I can’t get the user information on a per post basis because all of that data is supposed to be fetched by the “smart” parent component.

So I’m curious about how I could go about getting that data and combining it with the post so that the dumb component doesn’t really have to do any work.


#5

Yes, you should be able to get any data you want. You just have to publish it, subscribe and return it from the container. For example, using createContainer:

import { Meteor } from 'meteor/meteor';
import { createContainer } from 'meteor/react-meteor-data';
import { PostPage } from '../../api/pages/PostPage.js';

export default createContainer(({ params }) => {
  const subscription = Meteor.subscribe('post');
  const loading = !subscription.ready();
  const allPosts = Post.find().fetch();

  return { 
     loading, 
     allPosts 
  };
}, PostPage);

The allPosts will load into this.props on PostPage.

So to get the user data per post, you may have to already have that data within the Post document as when you save the post.

        let postToSave = {
          poster_hsid: this.userId,
          poster_username: username,
          poster_photo: photo,
          post_date: new Date(),
          post_text: status,
        }; 

        Post.insert(postToSave);

I found that easier than having to join collections using one of the join packages. You can also embed an array objects (such as comments) within the Post collection. But the issue I have with that is that if the user updates their info, then the Post document would be out of date. So newly, I’m learning Apollo because it’s easier to get all that data in one query without duplicating it throughout collections.


#6

Thanks for the thorough response! I try to avoid duplicating data and having to perform updates in multiple places should that data change.

Maybe Apollo is the only answer right now, but I don’t think so. I’ve done this with Blaze and I think for React it’s just a matter of finding the pattern.


#7

Cool. It would be nice to see what you find.:slight_smile: Or if anybody else have some insight/pattern maybe they will chime in.


#8

Bumping this. One of TheMeteorChef guys said something about publishComposite.

Could any share about their experiences with that…? And if not, are there any other patterns out there?


#9

I’ll help you bump it again :grin: …but thought about this forum post when I saw this new package https://github.com/cult-of-coders/grapher