【问题标题】:Meteor: Creating a collections: Reference Error when debug in chrome consoleMeteor:创建集合:在 chrome 控制台中调试时出现参考错误
【发布时间】:2015-01-26 03:41:02
【问题描述】:

我遵循Meteor 的教程,我尝试为客户端和服务器创建一个集合。这是我的代码:

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。但是当我在我的机器上尝试时,我收到了错误:

参考错误。列表没有定义

我做错了吗?请告诉我。

谢谢:)

【问题讨论】:

  • 仅供参考,Mongo.Collection 是新的Meteor.Collection

标签: node.js meteor


【解决方案1】:

您还可以将所有收藏放在/lib/collection.js 路由中(以获取更好的做法)。

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

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

meteor remove autopublish
meteor remove insecure.

所以一个简单的集合看起来像这样。

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

// 这里我们控制集合的安全性。

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

只是尝试更深入地解释一下关于流星的Collection,希望它对您有所帮助

【讨论】:

    【解决方案2】:

    我认为您已关闭自动删除/自动订阅。试试

    if (Meteor.isClient) {
        Meteor.subscribe('lists');
    }
    
    if (Meteor.isServer){
        Meteor.publish('lists',function(){
            return Lists.find();
        });
    }
    

    对于您的命名,我还建议您颠倒收藏集的大写方式。所以改为

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

    最后,查看https://github.com/matteodem/meteor-boilerplate 的目录结构,这样您就不必再执行 if meteor.is 的操作了。

    编辑

    完整代码应如下所示:

    var Lists = new Meteor.Collection("lists");
    
    if (Meteor.isClient) {
        Meteor.subscribe('lists');
    }
    
    if (Meteor.isServer){
        Meteor.publish('lists',function(){
            return Lists.find();
        });
    }
    

    【讨论】:

    • 请您在“列表”和“列表”之间再次小心。因为在我输入上面的代码并将Lists 反转为lists 之后,我仍然得到相同的错误结果。谢谢:)
    • 我更新了我的回复以获得完整的代码。但是,请注意您在做什么。复制和粘贴不是一个好的学习策略。看看meteor订阅的是什么,是你创建的变量还是你创建的集合?它在发布什么?集合还是变量?
    【解决方案3】:

    作为构建过程的一部分,您的所有脚本源文件都是wrapped in a function closure。为了使您的集合在该文件之外可见(或者在您的情况下 - 附加到 window 对象),您需要将其声明为全局变量:

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

    请注意缺少var。正如@thatgibbyguy 指出的那样,公认的模式是集合变量和驼峰式集合名称大写。

    【讨论】:

    • 啊。很好:) 我明白你的意思。我现在可以解决我的问题了:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    • 2019-07-08
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多