0
votes

I have two Collections in Meteor, and trying to join them. As defined in collections/collections.js

Producers = new Mongo.Collection('producers');
Projects = new Mongo.Collection('projects');

Conceptually, one Producer has 0 to many Projects. One Project must have a Producer. So, a ProducerID field is in each Project document (row) in Mongo. I seeded the Mongo database with data.

When my template for viewing Projects is displayed, I want it to have access to the Producer's attributes.

In Iron Router's config (in /app.js), I have

Router.route('project', {
  path: '/project/:name',
  template: 'project',
  waitOn: function() {
    return Meteor.subscribe('ProjectInfo', this.params.name);
  },
  data: function() {
    return Projects.find();
  }
});

I have two publishes on the server (in /server/publish.js):

Meteor.publish("ProjectInfo", function(projectName) {
  return Projects.find( {name: projectName} );
});
Meteor.publish("ProducerInfo", function(producerid) {
  return Producers.find( {_id: producerid});
});

Question 1: How to join? I definitely don't want to just throw Producer data into each Project, because it makes it harder to update the database when a Producer data changes.

Question 2: Why does Iron Router need a "data:" field, when it already has a Subscribe in the waitOn?

data: function() {
  return Projects.find();
}

Thanks in advance.

1

1 Answers

0
votes

How to join: I'll just give you link to package doing just that: publish with relations

And you don't need data field when you publish, if your router loads OK and you have waitOn with publishes then on client side all your data should be visible when you do Producers.find() or Projects.find()

Although, waitOn requiers an array, so try

return [Meteor.subscribe('ProjectInfo', this.params.name)];