0

I have a test app based on the meteor leaderboard example which works but when I create my own app, I can't display data and the queries don't work. If I run meteor mongo in a terminal window I can see that there is data being inserted.

db.games.find();

displays all the games.

db.games.remove(); 

deletes all the games but neither work inside my meteor app.

Nothing in this app works except the inserts.

if (Games.find().count() === 0)

always returns zero.

Here is my main.js file.

var court = 1;

var currentDate = new Date;

var gameDate = new Date("2014-10-19");

var gameTime = new Date("21:15");

var MinutesSinceMidnightNow = (new Date() - new Date().setHours(0,0,0,0)) / 6000;

var MinutesSinceMidnightGameTime = (new Date().setHours(21,15,0,0) - new Date().setHours(0,0,0,0)) / 6000;

Games = new Meteor.Collection("games");

if (Meteor.isClient) {

  Template.game.helpers({
    games: function () {

      return Games.find({});

    }  // players func
  });  //template helpers

 Meteor.startup(function () {
  if (Games.find().count() === 0) {
    Games.insert({
     "court_id": court,
     "game_date": gameDate,
     "court_name": 'Court 1',
     "game_time" : gameTime,
     "team_a": "bulldogs",
     "team_b": "sea eagles",
     "team_a_score": 0,
     "team_b_score": 0,
     "game_duration": 75,
     "game_half": 0,
     "game_quarter": 15,
     "quarter_time": 3,
     "half_time": 5,
     "game_minutes": MinutesSinceMidnightGameTime
     });
   }
 });

}  // isClient

if (Meteor.isServer) {
  Meteor.startup(function () {

  Games.remove();

  });
}

Here is my HTML file.

<head>
  <meta name="viewport" content="width=device-width, user-scalable=no">
</head>

<body>
  <div class="outer">
    {{> scoresheet}}
  </div>
</body>

<template name="scoresheet">
  <div class="scoresheet">
    {{#each games}}
      {{> game}}
    {{/each}}
  </div>
</template>

<template name="game">
  <div class="game">
  <h1>"Hello"</h1>
    <span class="name">{{game_time}}</span>
    <span class="name">{{court_name}}</span>
    <span class="name">{{team_a}}</span>
    <span class="name">{{team_b}}</span>
    <span class="score">{{team_a_score}}</span>
    <span class="score">{{team_b_score}}</span>
  </div>
</template>
markhorrocks
  • 1,199
  • 19
  • 82
  • 151
  • Have you removed the autopublish package? If so, you'll need to subscribe to some data to see it on the client. – richsilv Oct 19 '14 at 12:12
  • to empty the collection you need to include the selector `{}` -- [see the docs#remove](http://docs.meteor.com/#remove) - so it would be `Games.remove({})` – garrilla Oct 19 '14 at 12:59
  • it just occured to me, putting the `.remove()` in the server startup will also lead to a race condition... you should at least move it into the normal server section but maybe add a timer as per my answer below... – garrilla Oct 19 '14 at 13:47

1 Answers1

0

you're running into a race condition...

the query Games.find() is running after the collection Games is declared but before the collection Games is sync'd so the answer will be 0 even when n > 0.

You can test is this is the case by running Games.find().count() in the console when your app has fully loaded. It should print n.

A solution, when you're expecting there to be docs in the collection is to wrap it in an autorun

Meteor.autorun(function() {
    if(Games.findOne()) {

      //do your stuff here
      }

  });

but this would still fail for your case of n=0

so you could put it in a timer and test for DDP._allSubscriptionsReady()

timer = Meteor.setInterval( function(){test();}, 200 );

test = function(){
  if(DDP._allSubscriptionsReady()) action(timer);
};

action = function(timer){
  Meteor.clearInterval(timer);

  /* now do stuff */
  console.log('subs are ready, really ready', Games.find().count())
};

However, in a real-world scenario you should consider removing the autopublish package and set up a publication/subscription and putting your code in the callback of the subscription as per this answer

Community
  • 1
  • 1
garrilla
  • 539
  • 5
  • 15
  • Autorun made no difference. The console reports the correct number of games but my app inserts regardless of how many games are in the db already. – markhorrocks Oct 19 '14 at 13:58
  • I need the client to test if records are loaded and get new records of not. Preferably, I would like to erase some records and refresh them from an APi on client start up. – markhorrocks Oct 19 '14 at 14:21
  • @markhorrocks i do exactly that in [this demo](http://fvrs.meteor.com/) - you can expose the code in your browser console... the only but you wont see is that I reset the collections in the server to empty with the `.remove({})` method – garrilla Oct 19 '14 at 16:51
  • @markhorrocks see the [gist here] of the `.js` file (https://gist.github.com/garrilla/f96be2490d158f1bf2e3)... – garrilla Oct 19 '14 at 16:58