To answer your primary question, the reason your Template is not iterating is because your contactList
function is simply not returning anything. And even if it did return something, it still would likely not work because of your approach. Unfortunately, the way to fix this is not just by adding a simple return
statement but rather changing your entire approach.
First of all, I would highly encourage you to read and follow the Blaze Tutorial from beginning to end and before returning back to your project.
Based on the sample code that you have shared, it is clear that you have misunderstood most of the Meteor basics (which is a shame because Meteor is an extremely powerful and enjoyable framework). Hopefully, I can help clear some things up, but it is definitely essential to understand how Meteor works before trying to jump in.
The biggest issue that I see here is that you are defining API endpoints and using them from your front end. While this is a fairly normal approach in other frameworks/technologies, the relationship between the server and client is completely different in Meteor. So different in fact that only an example will be able to demonstrate this difference.
Based upon what you provided in your question, I have re-written everything to explain how to approach this in Meteor.
First would be the template definition (no real change here).
<template name="manageContacts">
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Title</th>
<th>Phone 1</th>
<th>Phone 2</th>
<th>Phone 3</th>
</tr>
</thead>
<tbody>
{{#each contact in contactList}}
<tr class="clickable" name="edit-contact" >
<td>{{contact.name}}</td>
<td>{{contact.email}}</td>
<td>{{contact.title}}</td>
<td>{{contact.phone1}}</td>
<td>{{contact.phone2}}</td>
<td>{{contact.phone3}}</td>
</tr>
{{/each}}
</tbody>
</table>
</template>
The next part however is quite different however. Please note that I don't really recommend combining server and client only code within the same file, but I did so in this instance to save space.
const Contacts = new Mongo.Collection('contacts');
if (Meteor.isServer) {
Meteor.publish('contacts', function() {
return Contacts.find();
});
}
if (Meteor.isClient) {
Template.manageContacts.onCreated(function() {
Meteor.subscribe('contacts');
});
Template.manageContacts.helpers({
contactList: function() {
return Contacts.find({
company_id: Session.get('company_id')
});
}
});
}
What we have going on here is the creation of a contacts mongo collection that will store all the contact data. We then define a publish function on the server that publishes all the contact data to the client. From the client (e.g. the template) we subscribe to the publication (this is how the template acquires its data) and provide a helper function (contactList
) to the Template that returns a mongo cursor. Blaze will of course be able to iterate over the cursor and all our contacts will render on the screen.