createContainer problem

Hello,
i have a problem,

export default createContainer(()=>{
  Meteor.subscribe('users');
  Meteor.subscribe('sample');

  return{
    users:Meteor.users.find({_id: Meteor.userId}).fetch(),
    sample_data: Sample.find({First_name: FirstName}).fetch()
  }
}, FinalReportsInformation);

how can i fetch the First Name (profile:{FirstName}) in users collection, and use it in sample_data?
thanks in advance.

Anyone who know how to solve my problem?

Mongodb is not relational in that way. You will have to store the profile data in the sample collection to get it. Or there are packages that help with relational data in Meteor mongo. Or use Apollo or Grapher https://github.com/cult-of-coders/grapher

1 Like

Try something like this:

export default createContainer(() => {
  let handleUsers = Meteor.subscribe('users');
  let handleSample = Meteor.subscribe('sample');

  let loading = !handleUsers.ready() && !handleSample.ready();
  let user = Meteor.users.find(Meteor.userId).fetch();
  let sampleData;

  if(user){
     sampleData = Sample.find({First_name: user.profile.FirstName}).fetch();
  }

  return { loading, user, sampleData };
}, FinalReportsInformation);

But actually the current user should be available as Meteor.user() so this should also work:

export default createContainer(() => {
  Meteor.subscribe('sample');

  let user = Meteor.user();

  return{
    user,
    sampleData: Sample.find({First_name: user.profile.FirstName}).fetch()
  }
}, FinalReportsInformation);

Some general recommendations:

  • try using handle.ready() to check if your subscription is ready, if not you can render some placeholders in your template.
  • style is personal, but I would recommend:
    • user.profile.FirstName => user.profile.firstName
    • First_name => firstName
    • sample_data => sampleData
    • users => user (since it’s only one…)

Hope this helps!

2 Likes

Look at meteor-publish-composite

1 Like