繁体   English   中英

使用Meteor时,我无法从客户端控制台访问该集合?

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

这是最奇怪的事情,由于某些原因即使启用了自动发布,我也无法从浏览器控制台访问该集合。 下面的代码是一个简单的列表程序,您可以在其中输入项目到集合中,它将在屏幕上显示为列表。 在控制台中,当我尝试通过键入People.find()或People.find()。fetch()来访问数据库时,它会显示错误“ReferenceError:找不到变量:People”

为什么会发生这种情况我自动发布,所以我想我可以从客户端访问该集合?

码:

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

}

除非你使用coffeescript,否则你不必使用@ 在普通的javascript中删除var这样你的变量就可以在文件外的任何地方访问(包括chrome控制台):

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

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

要使用coffeescript,请使用.coffee扩展名并运行meteor add coffeescript以允许meteor将coffeescript文件编译为js文件

回答你的问题@用于CoffeeScript(Javascript Alternative),你可以为它添加meteor包,然后用该语言编写你的应用程序。 http://coffeescript.org了解它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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