Meteor无法访问Collection

时间:2015-12-12 14:43:01

标签: javascript mongodb meteor

我正在经历我的第一个Meteor构建,我到了某个角落然后撞到了墙上。我无法访问我的集合,起初我认为这是我的查询语法的问题,但我认为不是。

我尝试过:db.collection.find()并且它在Mongo shell中按预期工作,但在客户端中,db.collection.find()返回LocalCollection.Cursor,它只是给我一个函数和原型的列表与当前集合相关联。

db.collection.find().fetch()只返回{}

更多信息:我安装了 admin-ui ,我通过它设置了架构。当前用户为Admin,已分配管理员角色。

提前感谢您的帮助。我正试着调试这个问题。

3 个答案:

答案 0 :(得分:1)

的核对表我无法在Meteor问题中查看我的数据

  1. $ meteor add autopublish
  2. 你现在看到你的数据吗?
  3. 型:

    console.log('My subscription has '+myCollection.find().count()+' documents!");
    

    在您的浏览器控制台中。

    1. 如果是,那么您尚未正确发布和订阅您的收藏集:
    2. 服务器:

      Meteor.publish('myPublication',function(){
        return myCollection.find(); // or whatever subset you want for this user
      });
      

      客户端:

      var handle = Meteor.subscribe('myPublication');
      
      1. $ meteor remove autopublish
      2. 你现在看到你的数据吗?
      3. 是 - 问题解决了
      4. 不,你过早检查吗?您可能需要等待订阅准备好。这通常在路由器中完成,但也可以通过检查订阅句柄的ready()状态来完成。
      5. 代码:

        if ( handle.ready() ) {
          console.log('My subscription has '+myCollection.find().count()+' documents!");
        }
        

答案 1 :(得分:0)

Mongo有一个专门用于mongodb操作的API,它与mongodb自己的API略有不同。

在流星世界,你首先要定义一个集合" (以最基本的形式在服务器和客户端上)

MyCollection = new Mongo.Collection('mycollection');

现在默认情况下,这最终会在名为" meteor"的数据库中结束。一个名为" mycollection"

的集合

插入:

MyCollection.insert({foo: "bar"});

要查询,假设您没有删除" autopublish"来自您的应用程序的包

MyCollection.find();

现在返回一个"光标"不是一个数组,所以要将它映射到一个数组,你做

MyCollection.find().fetch();

然而在mongo shell上,你会完成

use meteor
db.mycollection.find()

答案 2 :(得分:0)

所以,经过一番思考,我意识到我应该解决与我的问题有关的问题,也许我是个白痴,但我学到了很多东西:

  1. 我的收藏中没有发布。
  2. 我的语法对我的数据库来说是不正确的,很难来回原型如何让它工作。
  3. 以下是我的帮助程序的代码:

    myFunction: function () {
    
        // get from array the date we need and its ISOdate
    
        selector = Session.get("myCalendarArray");
        today = selector.month[dayPointer];
        today = today["ISOdate"];
    
        //  set the date for tomorrow, in this case the dates are set at 12:00AM,
        //  so we actually only need half way through the day.
    
        tomorrow = new Date();
        tomorrow = tomorrow.setDate(today.getDate() + .5);
    
    
        return MyCollection.find( { releaseDate : { $gte : new Date(today), $lte : new Date(tomorrow) } }  ).fetch()
    
        }
    

    所以我最终要解决的问题是:

    1. " releaseDate"指针在引号中,显然需要裸露,与shell等不同。
    2. new Date(today)是从数据库中选择日期的正确方法$ date不起作用。