Trying to get data out of a collection help!

So, i’m fairly new to meteor and this is my first backend work so I’m not massively familiar with all the methods of working with databases. But my issue is, I’m building a logging system where you log data each day and there are six fields of logged data. Im trying to build a pie chart with chart js which is working but i can’t get the correct data out of my database to make the pie chart.

So I have a collection called dailyLog, this is in my ‘both’ directory,

dailyLog = new Mongo.Collection('dailyLog');

then i publish this from the server

Meteor.publish("dailyLog", function () {
return dailyLog.find();
});

Then in my pieChart js file i have this code (client side)

Meteor.subscribe("dailyLog");

function drawPieChart(){

var findData = function(){
	return dailyLog.find({createdBy: Meteor.user().username}, {fields: {adherence: 1}}).fetch()[0].adherence;
};

var data = [
	{
		value: findData(),
		color: "#CBDDE7"
	}...

So the key bit of information is the find() method. So in each of these dailyLog i have the createdAt date and createdBy, and then each of the 6 variables and the one you see here is adherence which is one of the key value pairs and basically i am literally trying to take its key value pair from the database to use as data in the pie chart!

Now, my issue is if i type that into the console i get the value (which i 90) but in the code it always comes out as undefined. Not only this but this happens:

[Log] TypeError: undefined is not an object (evaluating 'dailyLog.find({createdBy: Meteor.user()}, {fields: {adherence: 1}}).fetch()[0].adherence') (meteor.js, line 888)

Thats the result when i console.log the dailyLog.find() method you see above.

I dont understand why it works when i put it in the console but the result is undefined in the code!

Thanks so much for your help in advanced and if you need any more information i will sat he ready to answer so let me know!

I guess you are trying to read data from the collection but the subscription is not ready yet (i.e. the data has not been loaded from the server yet). You need to wait for the subscription to be ready before calling find.

Any idea on how to do that? Im using iron router so i could set the waitOn parameter but when I’m normally loading it how would i go about that in the code above? Cheers

If you are fairly new to meteor, don’t use Iron Router. Or at least don’t use Iron Router hooks. And don’t use Iron Router subscription mechanism.
One way to wait for subscriptions to be ready is to use the subscriptionsReady function. See here.

I am used to angular route provider so the basics of it are fine but i will hold for the hooks for now, any reason why i should avoid them? So far it has been a pleasant experience with what i have done so far!

Great, i will have a look at that now, thanks a lot for your help Steve!

Im still not getting this, most of these seem to just delay the display or the template, not actually stop the javascript running, is there any way and i can make the data search only run once its done not just display a loading template?

You have to wait for the data to be ready before executing the query. And you have to decide what to display while waiting.

How could i go about doing that though? is there an isReady style call i could make to know when to make the query?

From my link above (with an additional helper):

Template.notificationList.onCreated(function () {
  this.subscribe("notifications");
});

Template.notificationList.helpers({
  getNotifications: function() {
    return Notifications.find(...);
  }
});

<template name="notificationList">
  {{#if Template.subscriptionsReady}}
    {{#each getNotifications}}
      <div>{{this.name}}</div>
    {{/each}}
  {{else}}
    Loading...
  {{/if}}
</template>
1 Like