Meteor:创建集合:在 chrome 控制台中调试时出现参考错误 [英] Meteor: Creating a collections: Reference Error when debug in chrome console

查看:50
本文介绍了Meteor:创建集合:在 chrome 控制台中调试时出现参考错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照Meteor 的教程进行操作 我尝试为客户端和服务器创建一个集合.这是我的代码:

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

作为我读过的教程,当在服务器上运行时,如果我打开 Chrome 控制台并输入 lists 我将收到 Meteor.Collection.但是当我在我的机器上尝试时,我收到错误:

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:

参考错误.列表未定义

我做错了吗?请告诉我.

Have I done something wrong? Please tell me.

谢谢:)

推荐答案

您也可以将所有集合放在 /lib/collection.js 路径中(以获得更好的实践).

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

因此,我们确保meteor 首先加载集合,并且它们将在客户端/服务器上可用.

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

您应该删除 Autopublish/insecure 包,以避免在加载时meteor 发送所有集合并控制谁可以或不可以插入/删除/更新集合.

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
    }

现在在 /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

这篇关于Meteor:创建集合:在 chrome 控制台中调试时出现参考错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆