user1665355
user1665355

Reputation: 3393

Use existing collection in meteor

I am trying to use an existing collection in meteor:

I tried

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

But that did not work. I modified the example from 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({});
          }
        });
      }
    }
  });
}

parties-list.html

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

main.html

<parties-list></parties-list>

Any ideas what I am doing wrong? I found the answer at Cannot connect to alternate Mongo DB in Meteor App but still trying to make the <ul> to show something, now it does not show anything and my page is empty.

Upvotes: 1

Views: 326

Answers (1)

Brendan Turner
Brendan Turner

Reputation: 430

I see a few problems:

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

This will only return one document, and I assume you want all of them since you're using ngRepeat to iterate over parties. This leads me to the next issue:

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

You're iterating over parties with ngRepeat which repeats the <li> for each object found within parties and assigns that value to a temporary object party. It should look like this:

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

Upvotes: 1

Related Questions