MiGu3X
MiGu3X

Reputation: 119

Subscribe to another publication after one has already loaded | React-Meteor

So I have 2 publications, teams.dashboard and boards.board, the thing is that, once the Boards fields have already loaded from teams.dashboard publication I want to load boards.board publication with the boardId from the first Board I have, for example: Boards.find().fetch()[0]._id.

Is there any React callback or Meteor way to subscribe to that once the props are ready? Because for some reason I'm subscribing to the data in the componentDidUpdate with a couple of 'ifs' and the websocket returns:

[{\"msg\":\"sub\",\"id\":\"R74JgCvWX8B8uphoZ\",\"name\":\"boards.board\",\"params\":[\"F4MuZ2jX8YKcf8fMr\"]}"]

but the next message is:

["{\"msg\":\"unsub\",\"id\":\"R74JgCvWX8B8uphoZ\"}"]

For some reason, the publication automatically unsubscribes... Subsequently, there are two websocket messages which say the following

[{\"msg\":\"changed\",\"collection\":\"Boards\",\"id\":\"F4MuZ2jX8YKcf8fMr\",\"fields\":{\"field1\":false,\"field2\":[],\"field3\":false,\"field4\":[],\"field5\":[]}}"]
[{\"msg\":\"changed\",\"collection\":\"Boards\",\"id\":\"F4MuZ2jX8YKcf8fMr\",\"cleared\":[\"field1\",\"field2\",\"field3\",\"field4\",\"field5\"]}"]

Upvotes: 0

Views: 68

Answers (1)

gaiazov
gaiazov

Reputation: 1960

You can use the callback parameter of Meteor.subscribe method to create another subscription.

Meteor.subscribe('teams.dashboard', () => {
    let firstBoard = Boards.findOne({}, {fields: {}});
    if (!firstBoard) {
        return;
    }

    Meteor.subscribe('boards.board', firstBoard._id);
}

of course, you have to manage your subscriptions. I'm not familiar if you can automatically unsubscribe in React components like Blaze can.

Upvotes: 1

Related Questions