Mongodb cursor's forEach does not execute

I have this code

var employeesEdited = [];
var employees =  Employees.find({group: 'ABC', task : { $in: ['CHANNEL MANAGER','SUPERVISOR','LINE LEADER'] }}
                    , {employeeNo: 1, firstName: 1, middleName: 1, lastName: 1, task: 1, supEmpNo: 1, supName: 1});

console.log(employees.count()); // I get 0 on this

employees.forEach((employee) => {
    console.log('inside for each'); // this does not run, no logs in browser
    employeesEdited.push(employee.employeeNo, employee.supEmpNo, 'test_tooltip');
});

employees.forEach((employee) => {
    console.log(`Emp no: ${employee.employeeNo}`);
});

console.log(employeesEdited);

What I am trying to do is to loop through the returned cursor of my collection using for each. But it doesn’t appear to execute. The console.log inside the forEach loop does not execute.

Also the Cursor.count() method returns 0.

BUT when I console.log the returned cursor from the find function, I can see that there are items in that object if I console.log(employees)

Not really sure what I am doing wrong here, I followed the syntax as per documentation in meteor.

Also I changed the code from using function() to using the fat arrow but still the same results

This code is inside the Template.template.onRendered event. Other codes run, but this forEach is not working for me.

I tried to access the the details in the Cursor via dot notation but for some reason when I reach the _map, I am not able to get the contents.

Please bear with me for I am a noob

Are you subscribed to any docs in Employees ?

A good tool for you to see what docs are subscribed is constellation.

meteor add babrahams:constellation

1 Like

I think in your query you’ve missed the field specifier and you’ve just gone and started which fields to include. You’re supposed to have {fields: {...}}

2 Likes

sweet tool, thank you. this will definitely make my job easier.

and yes I am subscribed to a publication for Employee

yep you are right. I had this corrected and still the same issue. I will update once I get to play more with constellation

It just sounds like you’re not correctly subscribed (or if you are - the subscription isn’t providing any data which matches your query). Remembering that the client will only be able to query data that it has been “given” by the server.

Try: Employees.find().count() do you still see 0?

I have been trying to understand more what you said: the client will only be able to query data that it has been “given" and played with the app.

I have two publications, one that gets all the employees, and another that gets only the employees of a certain group.

what happens is that when I first load the page that gets all the employees, and then switching to my page for a specific list of employees to be loaded to the org chart, it works.

but if I go straight to the org chart without first visiting the employee all list, it doesn’t work.

not sure why is that, my two publications are mostly the same besides the filter for the group.

Ah so you’re subscribing to the same collection twice in the same application?
That is a funny area I am not 100% sure on the behaviour.

I would be asking whether or not you need this data to be re-active or not…
If not - you are probably better off getting it via a method call.
It is less work on the server and much easier to debug because it behaves more like an API call.

1 Like

Yes this is what I was trying to do.

Ok I will try to use methods since the data doesn’t have to be re-active. Thank you for help, appreciate it.