简体   繁体   English

客户端无法访问Meteor MongoDb集合

[英]Meteor MongoDb collection not accessible on client side

I have created a collection which should be accessible to client side and server side. 我创建了一个客户端和服务器端都可以访问的集合。 But when I try to use it in browser it gives me undefined. 但是,当我尝试在浏览器中使用它时,它给我未定义的含义。

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

Now when I use lists in client side browser console it gives me undefined. 现在,当我在客户端浏览器控制台中使用列表时,它给了我未定义的信息。

Define the collection without var keyword. 定义不带var关键字的集合。 It will make a global variable accessible in the whole application. 它将使全局变量可在整个应用程序中访问。 And define collections uppercase: 并定义大写的集合:

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

It's a good practice. 这是一个好习惯。

If you has removed autopublish package, you should subscribe on collection at the client-side 如果已删除autopublish程序包,则应在客户端订阅集合

Meteor.subscribe("lists");

publish it at server-side 在服务器端发布

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

and use lowercase for collection name. 并使用小写字母作为集合名称。

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

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