繁体   English   中英

客户端无法访问Meteor MongoDb集合

[英]Meteor MongoDb collection not accessible on client side

我创建了一个客户端和服务器端都可以访问的集合。 但是,当我尝试在浏览器中使用它时,它给我未定义的含义。

var lists = new Meteor.Collection("Lists");
//lists.insert({Category:"DVDs", items: {Name:"Mission Impossible",Owner:"me",LentTo:"Alice"}});
if (Meteor.isClient) {
  // counter starts at 0
  Session.setDefault('counter', 0);

  Template.hello.helpers({
    counter: function () {
      return Session.get('counter');
    }
  });

  Template.hello.events({
    'click button': function () {
      // increment the counter when button is clicked
      Session.set('counter', Session.get('counter') + 1);
    }
  });
}

if (Meteor.isServer) {
  Meteor.startup(function () {
    // code to run on server at startup
  });
}

现在,当我在客户端浏览器控制台中使用列表时,它给了我未定义的信息。

定义不带var关键字的集合。 它将使全局变量可在整个应用程序中访问。 并定义大写的集合:

Lists = new Meteor.Collection("lists");

这是一个好习惯。

如果已删除autopublish程序包,则应在客户端订阅集合

Meteor.subscribe("lists");

在服务器端发布

Meteor.publish("lists", function () {
  return Lists.find({});
});

并使用小写字母作为集合名称。

暂无
暂无

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

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