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.
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())
The problem is that you have to wait for data from the server. When you just use Template.name.rendered
function it is immediately invoked. You have to use Template.name.helpers
function to wait for data from the server. Everything is described in the documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With