我有一个集合,我用种子填充,看起来像这样
if (Schools.find().count() === 0) {
school1Id = Schools.insert({
name: "High School For Boys",
housename: {
house1: 'Milton',
house2: 'Granton',
house3: 'Buxton',
house4: 'Barnard'
}
});
school2Id = Schools.insert({
name: "Pretoria Boys High",
housename: {
house1: 'House1',
house2: 'House2',
house3: 'House3',
house4: 'House4'
}
});
我有另一个像这样播种的集合
if (Years.find().count() === 0) {
for (y = 1905; y < 2015; y++ ) {
Years.insert({
year: y
});
}
}
我还有一个看起来像这样的集合
if (Meteor.users.find().count() === 0) {
var school1Id = Schools.findOne({name: "High School For Boys"})._id;
var school2Id = Schools.findOne({name: "Pretoria Boys High"})._id;
seed1UserId = Accounts.createUser({
username: 'lbob',
email: 'l@oo.com',
password: '123456',
profile: {
firstname: 'Lance',
lastname: 'Bones',
phone: '0000000000',
schoolName: school1Id,
firstschoolyear: Years.findOne({year: 1984})._id,
lastschoolyear: Years.findOne({year: 1988})._id,
matriculated: 1,
housename: Schools.findOne(school1Id).housename.house1,
country: 'South Africa',
cityofresidence: 'Cape Town',
emplindustry: 'IT',
joined: new Date() // current time
}
});
seed2UserId = Accounts.createUser({
username: 'david',
email: 'd@oo.com',
password: '123456',
profile: {
firstname: 'David',
lastname: 'McNulty',
phone: '0000000000',
schoolName: school1Id,
firstschoolyear: Years.findOne({year: 1988})._id,
lastschoolyear: Years.findOne({year: 1990})._id,
matriculated: 0,
housename: Schools.findOne(school1Id).housename.house1,
country: 'Scotlad',
cityofresidence: 'Glasgow',
emplindustry: 'Hospitality',
joined: new Date() // current time
}
});
seed3UserId = Accounts.createUser({
username: 'glenn',
email: 'g@oo.com',
password: '123456',
profile: {
firstname: 'Glenn-Douglas',
lastname: 'Jones',
phone: '0000000000',
schoolName: school1Id,
firstschoolyear: Years.findOne({year: 1981})._id,
lastschoolyear: Years.findOne({year: 1986})._id,
matriculated: 1,
housename: Schools.findOne(school1Id).housename.hous1,
country: 'South Africa',
cityofresidence: 'Cape Town',
emplindustry: 'Coaching',
joined: new Date() // current time
}
});
seed4UserId = Accounts.createUser({
username: 'martin',
email: 'm@oo.com',
password: '123456',
profile: {
firstname: 'Martin',
lastname: 'James',
phone: '0000000000',
schoolName: school2Id,
firstschoolyear: Years.findOne({year: 1983})._id,
lastschoolyear: Years.findOne({year: 1987})._id,
matriculated: 1,
housename: Schools.findOne(school2Id).housename.house2,
country: 'Austrailia',
cityofresidence: 'Melbourne',
emplindustry: 'Airline',
joined: new Date() // current time
}
});
}
学校模板看起来像这样
<template name="schoolList">
<div class="title">Schools List</div>
<div class="list-group">
{{#each schools}}
{{> school}}
{{/each}}
</div>
<template>
<template name="school">
<a href="#" class="list-group-item school">
<!-- <span class="badge">44</span> -->
<span class="badge">{{totalalumnis}}</span>
{{name}}
</a>
<template>
我的js文件看起来像这样
Template.schoolList.helpers({
schools: function () {
return Schools.find({});
},
totalalumnis: function () {
// return Schools.find({}'_id');
//return Meteor.users.findOne()
return Schools.find({}).count();
}
});
我正在尝试计算每所学校的学生人数并将其显示在徽章中
从js文件中可以看出,我很难理解如何设置查询的最佳方法。 我不确定我是否错误地创建了数据,我宁愿将学生存放在学校集合中。我的逻辑告诉我,这会更容易计算并且更具反应性,但我决定将学校ID存储在学生中,其逻辑是所有关于学生的信息都在他们的用户文档中。
我还希望在另一个页面上列出学校每年复制此类搜索的学生人数,以便我可以将该结果添加到年份徽章中。
我真的很感激帮助。
编辑:我对帮助代码做了一些修改,看起来像这样
Template.schoolList.helpers({
schools: function () {
return Schools.find({});
},
totalalumnis: function () {
return AlumniList.find({schoolName: this._id}).count();
}
});
我已在浏览器控制台中测试了该查询,如果我提供了有效的schoolName ID,则会返回正确的学生数。但是,这不会显示在模板中。
有人可以帮忙吗?