I'd like to display data from a collection with a simple nvd3 discrete bar chart.
When i tried it with a local collection it worked just fine. Now i shifted the same data to a db collection but i can't get the data inside Meteor's .rendered.
Template.chartPopularWordsAll.onCreated(() => {
let template = Template.instance();
template.autorun(() => {
template.subscribe('dataViewed'); // DataViewed.find()
});
Template.chartPopularWordsAll.rendered = function() {
let data = DataViewed.find({}, {
limit: 5,
sort: {
timesViewed: -1
}
}).fetch();
console.log(data); // <-- this returns an empty array
}
Question: How can i access the data inside the .rendered ?
In the Meteor docs searching for '.rendered' gives no results i can only find .onRendered. Is .rendered even up-to-date or is it deprecated?
Thanks in advance!
Muff
I believe the issue here is that mix up autorun for subscribing and fetching:
the autorun is ran when the data changes, so it's not the subscribe that needs to be inside the autorun, but the data lookup.
try this:
Template.chartPopularWordsAll.onCreated(() => {
let template = Template.instance();
template.subscribe('dataViewed'); // DataViewed.find()
});
Template.chartPopularWordsAll.rendered = function() {
template.autorun(() => {
let data = DataViewed.find({}, {
limit: 5,
sort: {
timesViewed: -1
}
}).fetch();
console.log(data); //
}
}
If that does not work, try to not fetch on the call to data, but fetch when you need the data: the Collection.find() gives you a cursor, that is reactive, however once you fetch you get an array, which is not reactive. The Collection.find() part 'should' be reactive within the autorun, but i'm not 100% sure.