user3312060
user3312060

Reputation: 1

Querying Meteor collection (works on the localhost, fails on server)

I have just started small project on Meteor, having a couple of months experience with it. I set up a collection called Reactions and imported csv data into MongoDB .

Now, when I query it from localhost (win 7) I got recordset as expected, but once I query it from remote server, I got emty set.

As it seemed to be a subscription timing issue, as the collection is about 10k records, I added 'waitOn' clause to a Iron Router route, but it did not help.

I searched for similar issues here and there and most suggested waitOn, so I have run out of ideas.

Please see below relevant pieces of code and thank you in advance for any hints

---------- / both / collections / Reaction.js  ----------------

Reactions = new Mongo.Collection('reactions', { idGeneration : 'MONGO'} );

---------- / server / publications.js  ----------------

    Meteor.publish('reactions', function() {
        return Reactions.find();
    });

---------- / client / views / subscriptions.js  ----------------

    Meteor.subscribe('reactions', function() {
          return Reactions.find();
    });

---------- / both / lib / router.js  ----------------

    this.route('reactions', {
        where: 'server',
        waitOn: function() { 
           return Meteor.subscribe('reactions');
        },
        action: function () { 
           var action = this.request.body.action; 
           var reactions = Reactions.find( { action: action } ).fetch();
           console.log( 'reactions.length = ' + reactions.length );

Upvotes: 0

Views: 91

Answers (1)

Michel Floyd
Michel Floyd

Reputation: 20226

Your route action needs to involve rendering. Your find() can run inside a data: function. And why the where: function? Subscriptions need to run on the client!

this.route('reactions', {
  waitOn: function() { 
    return Meteor.subscribe('reactions');
  },
  action: function () {
    this.render('myLayout'); 
  }, 
  data: function(){
    var action = this.request.body.action;
    return Reactions.find( { action: action } ); // you don't need to .fetch(), just return a cursor
  }
});

Upvotes: 0

Related Questions