Search code examples
jquerymongodbmeteor

Meteor rendered and mongodb collection


Am new to Meteor. I am trying to display data from mongoDB collection and want to iterate it to perform some calculation.

As mentioned bellow:

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

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

But am getting an empty array. How to handle this situation?

(I can put it on 'helper and subscriber portion', but I need to perform some jQuery operation using embedly. But this jQuery not working in this helper function.)


Solution

  • First you should know that when you declare Mongo collection is better to use the same name "employee" in the constructor.

    Also you should notice that in with 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); });
    }