Chartjs-Meteor, ChartsJS and MongoDB

1👍

You can do this

var teamNames = Teams.find().map(
   function(team){
      return team.name;
   }
)

0👍

teams must have a length of less than 10 items. If teams is [{name: "first"}], then teams[1] will return undefined and you will get that error. You can use:

for (let i = 0; i < teams.length; i++)

to solve this problem.

You can also map over the array to get specific properties:

labels: teams.map(team => team.name),

0👍

In Meteor, the Collection .find() function returns a cursor that you can then use to perform operations on collection items. In your case, you are treating the cursor as if it were an array which is incorrect. There are a few different ways that you can approach this.

1) Use .forEach() to iterate over the cursor.

var teamNames = [];

Teams.find().forEach(function (e) {
  teamNames.push(e.name);
});

2) Use .fetch() to return all matching documents in an array, then iterate over that.

var teams = Teams.find().fetch();
var teamNames = [];

for(i = 0; i < teams.length; i++) {
  teamNames.push(teams[i].name);
}

3) Use .map() to iterate over the collection calling the callback on all items and returning an array.

var teamNames = Teams.find().forEach(function (e) {
  return e.name;
});

Leave a comment