Helper method
GetSchoolId: function () {
var moduleIdArray=[];
var myslug = FlowRouter.getParam('myslug');
var mySchoolId = SchoolDb.findOne({slug: myslug});
if (mySchoolId) {
ModuleSchool.find({}, {schoolId: mySchoolId._id}).forEach(function (modulesSelected) {
moduleIdArray.push(modulesSelected.moduleId);
});
if (typeof moduleIdArray === 'object' && moduleIdArray instanceof Array) {
console.log(moduleIdArray);
moduleIdArray.forEach(function (moduleIds) {
return Modules.find({}, {_id: moduleIds}).fetch();
});
}
}
}
Template code:
{{#each GetSchoolId }}
<p>{{GetSchoolId.modulename}} </p>
{{/each}}
</p>
{{/each}}
I know to Meteor Profs, it is a inch of an iceberg, in seconds it will be trashed. I have 3 collections, one for school record (SchoolDb
), second for for module (Modules), and the third, a relationship table (ModuleSchool
). modules are assigned to schools.
From the code above, I am able to get the school _id from (SchoolDb
) using the slug passed to the route which I used to fetch the schoolId
from the relationship table (SchoolDb
) and the (ModuleSchool
) to return modules assigned to a the school in question. I was able to fetch the module Ids and converted them into arrays, what I now want to do is using the array of Ids fetched from the ModuleSchool
to return the module names from the Modules because only the _ids
are stored in the relationship table.
The above code does only does it to the level of converting the _ids to array, when I tried printing on the template nothing showed. What wrong am I to right?
To find document with a field corresponding to at least one element of an array you can use
$in
:And then simply use the
{{each}}
tag.But if each module is attached to only one school, i suggest a simpler solution for your problem, you don't have to create a collection between school and module.
You just have to create two collections:
Schools
andModules
and add to the Modules documents aschoolId
field.Then your code would look like that :