【问题标题】:Use existing collection in meteor在流星中使用现有集合
【发布时间】:2026-02-04 02:50:02
【问题描述】:

我正在尝试使用流星中的现有集合:

我试过了

SET MONGO_URL="mongodb://localhost:27017/test2" meteor

但这没有用。我从www.angular-meteor.com修改了示例:

server.js

if (Meteor.isServer) {
  Meteor.startup(function () {
    var myDatabase = new MongoInternals.RemoteCollectionDriver("mongodb://127.0.0.1:27017/test2");
    MyCollection = new Mongo.Collection("mydata", { _driver: myDatabase }); 
  });
}

app.js

if (Meteor.isClient) {
  angular.module('socially', ['angular-meteor']); 
  angular.module('socially').directive('partiesList', function() {
    return {
      restrict: 'E',
      templateUrl: 'parties-list.html',
      controllerAs: 'partiesList',
      controller: function($scope, $reactive) {
        $reactive(this).attach($scope);   
        this.helpers({
          parties: () => {
            return MyCollection.findOne({});
          }
        });
      }
    }
  });
}

party-list.html

<ul>
  <li ng-repeat="party in partiesList.parties">
    {{MyCollection.Date}}
    <p>{{MyCollection.name}}</p>
  </li>
</ul>

main.html

<parties-list></parties-list>

任何想法我做错了什么?我在Cannot connect to alternate Mongo DB in Meteor App 找到了答案,但仍然试图让&lt;ul&gt; 显示一些东西,现在它没有显示任何东西而且我的页面是空的。

【问题讨论】:

  • 你能注释掉那个 isServer 块并试试这个:% MONGO_URL=mongodb://localhost:27017/test2 meteor
  • @Brendan Turner 试过了,但没用
  • 错误信息? db 'test2' 是否已创建?
  • 没有错误。是的 test2 db 存在于带有 mydata 集合的本地 mongodb 中。但总的来说,这是一个正确的方法吗?或者环境变量也应该起作用?
  • env 是我在生产中使用我的应用程序的方式。不知道为什么它不起作用。您是否使用过 mongo shell 来确保 test2 上有集合/文档?

标签: angularjs mongodb meteor


【解决方案1】:

我发现了一些问题:

      parties: () => {
        return MyCollection.findOne({});
      }

这只会返回一个文档,我假设您想要所有文档,因为您使用 ngRepeat 来迭代 parties。这就引出了下一个问题:

<ul>
  <li ng-repeat="party in partiesList.parties">
    {{MyCollection.Date}}
    <p>{{MyCollection.name}}</p>
  </li>
</ul>

您正在使用 ngRepeat 对各方进行迭代,它为在各方中找到的每个对象重复 &lt;li&gt;,并将该值分配给临时对象 party。它应该是这样的:

<ul>
  <li ng-repeat="party in partiesList.parties">
    {{party.Date}}
    <p>{{party.name}}</p>
  </li>
</ul>

【讨论】:

  • 我试过了,但还是不行。当我在控制台中执行MyCollection.find({}).fetch() 时,我得到[]。这很奇怪,因为我知道在mydata 集合中的test2 数据库中有文档。