publishComposite: When is data available?


#1

Hey guys,
something is confusing me when using React-Native-Meteor with publishComposite. Assuming I have the following publish function:

publishComposite('allUserData', {
    find() {
        return User.find({_id: this.userId}, {fields: {'details': 1, 'username':1}}); 

    },

    children: [

        {
            find() {
                return Friends.find({userId2: this.userId});
            },
            
            children: [
                find(friendDoc) {
                    return User.find({_id: friendDoc.userId1});
                }
            ]
        }
    ]
    
    });

Within my React component, I do something like this

Friends.find().fetch().map((friendDoc) => <FriendItem userDoc={User.findOne(friendDoc.userId1)}/>);

Now, on some reloads, I run into trouble. My app tells me, that userDoc is null. So the data of User.find({_id: friendDoc.user1}); seems not to be added at the same time like the data of Friends.find({userId2: this.userId});. Since React-Native-Meteor does not use the “real” Livedata/Minimongo system of Meteor, I’m wondering if this behavior is the same in a normal Meteor application?

PS: I know that there is no createContainer function in my code to provide reactivity. In my real project I’m using it, it’s just to show you how I run into that null object (because I’m working with data from another collection which is published by the same publication but not available at the same time).


#2

So nobody knows how this stuff works :joy:? I just need to update my knowledge about this issue because I wrote most of my application with the information that this data is available at the same time and now on RN I see that this may be not the case.


#3

I have been running into this issue using publishComposite. So far I have not found a solution. Did you manage to fix it ? This issue is being discussed on Github https://github.com/englue/meteor-publish-composite/issues/67


#4

Well, the only way I got it to work is to check if the sub document is available before I do the rendering, so f.e.:

render() {
  if(!this.props.userDoc) return null;
  ....
}

If the sub document is available, render will be called again (I have a Meteor container as HOC). I guess the real problem is the order how Meteor sends the data to the client. Normally we need to send the sub documents before the parent documents. In our case, Meteor sends the parents first, while their subs are not instantly available.