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

标签 mongodb collections meteor

这是最奇怪的事情,出于某种原因,即使打开了自动发布,我也无法从浏览器控制台访问该集合。下面的代码是一个简单的列表程序,您可以在其中将项目输入到集合中,它会在屏幕上显示为列表。在控制台中,当我尝试通过键入 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 文件

关于mongodb - 使用 Meteor 时,我无法从客户端控制台访问集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18039458/

相关文章:

vba - 撤消集合中的项目

java - 为什么在创建迭代器对象后无法向 java 中的 ArrayList 添加值

javascript - Meteor.call ("function",arg) 不同步发生

javascript - AngularJS 在 ng-repeat 中更改变量

mongodb - 像 mongoDB 中的查询

python - 为什么我会收到 TypeError : 'module' object is not callable in python?

vba - 在 Excel 中使用 VBA 通过索引 # 选择字典项

javascript - 找到对象但文档属性未定义

javascript - 升级到 Meteor.js 1.3 后,在 Windows 上运行 Meteor 会出现 "You' re not in a Meteor 项目目录”

javascript - 为什么 mongodb .deny() 不起作用(Meteor)