[英]Meteor : getting data from collection
(这是我在流星上的第一个应用程序)我有问题,我无法从收集中获取数据。 我要去做日历。 我有一个集合,其中有天的名称,并且这个月有几天的数组,mondey是0等。在集合中,我有名称和索引,如果索引等于数组中的天数,我想显示天的名称不知道我是否做得很好。 注意我所有的应用程序代码,并在文件夹中进行了排序,但是我只是在需要帮助的地方复制了代码
<template name="calendar">
{{#each months}}
<span>{{name}}</li>
{{/each}}
</ul>
{{days}}
<div class="calendar">
<ul>
{{#each daysofweek}}
<li>
{{index}}
</li>
{{/each}}
</ul>
{{#each generateCalendar}}
<ul>
<li>
<ul>
{{#each this}}
<li>
{{day}}: {{dayOfWeek}}
</li>
{{/each}}
</ul>
</li>
</ul>
{{/each}}
</div>
<!-- <button class="remove">click</button>-->
</template>
db = new Array;
db['dayoftheweek'] = new Mongo.Collection('dayoftheweek');
db['months'] = new Mongo.Collection('months');
Template.calendar.helpers({
'daysofweek': function () {
return db['dayoftheweek'].find();
},
'generateCalendar': function () {
var tab = [];
var daysInMonth = Session.get('currentMonthDays');
var fDay = Session.get('currentMonthFDay');
for (var i = 1; i <= daysInMonth; i++) {
tab[i] = {day: i, dayOfWeek: fDay};
if (fDay === 6) {
fDay = 0;
} else {
fDay++;
}
}
var weeks = [];
var i = 0;
while (tab.length > 0) {
if (i === 0) {
weeks[i] = tab.splice(0, (8 - Session.get('currentMonthFDay')));
i++;
} else {
weeks[i] = tab.splice(0, 7);
i++;
}
}
return weeks;
}
});
Meteor.methods({
'removeFromDb': function(selected, dbname){
db[dbname].remove(selected);
}
});
我不确定我是否正确理解您的问题,但我认为您缺少该collection.find()
方法返回游标的方法。 因此,如果要获取数据,则需要调用cursor.fetch()
方法,该方法将向您返回一个包含数据的数组。
检查查找方法的文档: http : //docs.meteor.com/#/full/find
提取方法文档: http : //docs.meteor.com/#/full/fetch
要打印数组中对象的所有名称,您需要循环:
var days = db['dayoftheweek'].find().fetch(); for(var i=0; i<days.length; i++) { console.log(days[i].name); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.