I have two collections:
Contracts = new Mongo.Collection('contracts');
Reminders = new Mongo.Collection('reminders');
These are structured in the database more or less like this:
Contracts:
{
"id": "4432234",
"contract": "C-42432432",
"description": "Description of contract",
"counterpart": "Company name",
"status": "awarded"
},
etc.
Reminders:
{
"name": "Contract expiring",
"type": "expiring",
"contract": "C-42432432",
"reminderDate": "2015-06-01",
"urgency": 3
},
etc.
The "contract"-name here are referring to a "contract"-name in the contract-collection. We can ha multiple reminders connected to the same contract. Therefore I want them in two different collections.
To get the contract-data I use:
<template name="example">
{{#each contracts}}
{{description}}
{{/each}}
</template>
corresponding js:
Template.example.helpers({
contracts: function() {
return Contracts.find();
}
});
This works ok and the result in this instance is Description of contract
.
But what do I do if I want to display the reminder-collection and get the corresponding data from the Contract-collection? In other words: I want to loop the reminder-collection and get the same output.
<template name="notworking">
{{#each reminder}}
{{description}}
<!-- Here I want the description from the Contract-collection -->
{{/each}}
</template>
corresponding js:
Template.notworking.helpers({
reminder: function() {
//?
}
});
You might be better off using
Contracts._id
to refer to a contract from theReminders
collection that way if the contract name and description change at some point you won't need to update all the related reminders.Contract:
Reminder:
Then if you want to list reminders and show related contract information you'd have:
HTML:
JS:
OR - if you want to keep your denormalized schema, then the
relatedContract
function would simply need to returnContracts.findOne({contract: this.contract})