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!