简体   繁体   中英

Meteor : getting data from collection

(It's my first app in meteor) I have problem I can't get data from collection. I'm going to make calendar. I have collection with names of days and have array with this month days and mondey is 0 etc. In collection i have name and index and I'd like to display name of day if index is equal to number of day in array I don't know if I do it good. It's note all my app code and it's sorted in folders but i just copied code where I need help

    <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);
    }
});

I am not sure if I understood your problem right but I think that you are missing that collection.find() method returns a cursor. So if you want to fetch the data you need to call cursor.fetch() method which will return you an array with the data.
Check the docs of the find method: http://docs.meteor.com/#/full/find
Fetch method docs: http://docs.meteor.com/#/full/fetch

To print all names of objects from the array you'll need a loop:
var days = db['dayoftheweek'].find().fetch(); for(var i=0; i<days.length; i++) { console.log(days[i].name); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM