Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

node.js - Meteor: Creating a collections: Reference Error when debug in chrome console

I follow a tutorial with Meteor I try to create a collection, both for client and server. Here is my code:

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

if (Meteor.isClient) {

}

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

As tutorial I have read, when run on server, if I open chrome console and type lists I will receive Meteor.Collection. But when I tried on my machine, I received error:

Reference error. lists is not define

Have I done something wrong? Please tell me.

Thanks :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Also you can put all your collections inside the /lib/collection.js route (for better practices).

So with that we ensure that meteor loads first the collections, and they will be available on both client/server.

you should remove Autopublish/insecure package, to avoid meteor sends all the collections when load and to control who can or not insert/remove/update on the collections.

meteor remove autopublish
meteor remove insecure.

So a simple collection will look like this.

    //lib/collection.js
    Example = new Mongo.Collection("Example") //we create collection global
    if(Meteor.isClient) {
     Meteor.subscribe('Example') //we subscribe both after meteor loads client and server folders
    }

now on /server/collections.js

Meteor.publish('Example', function(){
          return Example.find(); //here you can control whatever you want to send to the client, you can change the return to just return Example.find({}, {fields: {stuff: 1}});
        }); 

// Here we control the security of the collections.

 Example.allow({
      insert: function(userId, doc) { 
        if(Meteor.userId()){
         return true; //if the user is connected he can insert
     } else{
    return false// not connected no insert
   }
 },
    update: function(userId, doc, fields, modifier) { //other validation },
    remove: function(userId, doc) { //other validation },
});

Just to try to explain a little more deep the Collection here on meteor, hope it help you GL


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...