Meteor.js Collection empty on Client

javascript, meteor, mongodb, node.js

Solution

You could use `count()` instead which returns the number of results. `data` itself would be an empty array, `[]` which isn't falsey ( `[] == true` ).

Also don't use `fetch()` unless you're going to use the raw data for it because its quite taxing. You can loop through it with `.forEach` if you need to.

var data = myCollection.find();

if(data.count())
  console.log(data);

//If you need it for something/Not sure if this is right but just an example
$('#chart').render(data.fetch())

Problem

Why is it that `myCollection.find().fetch()` returns an empty array `[]` even though the call is made within `if(data){...}`? Doesn't the `if` statement ensure that the collection has been retrieved before executing the `console.log()`? ``` Template.chart.rendered = function() { var data = myCollection.find().fetch(); if(data) { console.log(data); } $('#chart').render(); } ``` This returns `[]` in the browser Javascript console.

Original source