Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using Meteor I cannot access the collection from client console?

This is the strangest thing, for some reason even with autopublish turned on, I cannot access the collection from browser console. The code below is a simple list program where you can enter items into the collection and it will appear as a list on the screen. In console when I try to access the database by typing People.find() or People.find().fetch() it brings up an error 'ReferenceError: Can't find variable: People'

How come this is happening I have autopublish on, so I thought I can access the collection from the client?

Code:

var People = new Meteor.Collection("people");


if (Meteor.isClient) {


console.log(People.find());

Template.personList.people = function () {
    return People.find();   
};

Template.personForm.events({
    'click button': function(e, t) {
        var el = t.find("#name");
        People.insert({ name: el.value });
        el.value = "";
    }
});

Template.person.editing = function () {
    return Session.get("edit-" + this._id); 
};

Template.person.rendered = function () {
    var input = this.find("input");
    if(input) {
        input.focus();
    }
};

Template.person.events({
    'click .name': function (e, t) {
        Session.set("edit-" + t.data._id, true);
    },
    'keypress input': function (e, t) {
        if (e.keyCode == 13) {
            People.update(t.data._id, { $set: { name: e.currentTarget.value }});
            Session.set("edit-" + t.data._id, false);
        }
    },
    'click .del': function (e, t) {
        People.remove(t.data._id);  
    }
});

}
like image 238
Nearpoint Avatar asked Dec 07 '22 06:12

Nearpoint


1 Answers

You don't have to use @ unless you're using coffeescript. In plain javascript remove the var so your variable can be accessed anywhere outside of the file (including the chrome console):

var People = new Meteor.Collection("people");

becomes

People = new Meteor.Collection("people");

To use coffeescript use a .coffee extension and run meteor add coffeescript to allow meteor to compile coffeescript files to js files

like image 173
Tarang Avatar answered May 23 '23 00:05

Tarang