Collecting meteors and collecting mongodb

I'm new to Meteor. I am trying to display data from a mongoDB collection and I want to iterate to do some calculations.

As mentioned below:

Employee = new Mongo.Collection("data");

Template.welcome.rendered = function() {
   var employee = Employee.find({}); 
   employee.forEach(function(emp){ console.log(emp.id); });
}

      

But I am getting an empty array. How to deal with this situation?

(I can put it on "helper and subscriber part", but I need to do some jQuery operation using the inline. But this jQuery doesn't work in this helper function.)

+3


source to share


1 answer


First, you should know that it is better to use the same name "employee" in the constructor when declaring a Mongo collection.

Also you should note that in the latest version of MeteorJS you should use Template.name.onRendered () instead of rendered which works for backward compatibility, but it will be deprecated.

One more thing. Don't forget about the underscore before id like this: _id



Try this code and see if it works:

Employee = new Mongo.Collection("employee");

Template.welcome.onRendered( function() {
   var employee = Employee.find({}); 
   employee.forEach(function(emp){ console.log(emp._id); });
}

      

0


source







All Articles