Methods Error complaining forEach please help

Hello all,

The methods below generate a bunch of error that I have no idea what it is. Please advice what is wrong with the methods. Thanks in advance.

My Methods:

Meteor.methods({
    getUnassignEmployees: function () {
        let assignedEmployee = OutletEmployees.find({});
        let assignedEmployeeIdArr = [];
        assignedEmployee.forEach(data => {
            assignedEmployeeIdArr.push(data.employee_id)
        });
        let unassignEmployees = Meteor.users.find({
            _id: {
                $nin: assignedEmployeeIdArr
            }
        })
        return unassignEmployees
    }
});

When call, it give following error:

W20181216-22:26:04.744(8)? (STDERR) (node:19196) UnhandledPromiseRejectionWarning: RangeError: Maximum call stack size exceeded
W20181216-22:26:04.783(8)? (STDERR)     at Object.keys.forEach.key (packages/ejson/ejson.js:594:27)
W20181216-22:26:04.783(8)? (STDERR)     at Array.forEach (<anonymous>)
W20181216-22:26:04.783(8)? (STDERR)     at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
W20181216-22:26:04.783(8)? (STDERR)     at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
W20181216-22:26:04.784(8)? (STDERR)     at Array.forEach (<anonymous>)
W20181216-22:26:04.784(8)? (STDERR)     at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
W20181216-22:26:04.784(8)? (STDERR)     at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
W20181216-22:26:04.785(8)? (STDERR)     at Array.forEach (<anonymous>)
W20181216-22:26:04.785(8)? (STDERR)     at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
W20181216-22:26:04.785(8)? (STDERR)     at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
W20181216-22:26:04.785(8)? (STDERR)     at Array.forEach (<anonymous>)
W20181216-22:26:04.786(8)? (STDERR)     at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
W20181216-22:26:04.786(8)? (STDERR)     at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
W20181216-22:26:04.786(8)? (STDERR)     at Array.forEach (<anonymous>)
W20181216-22:26:04.786(8)? (STDERR)     at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
W20181216-22:26:04.787(8)? (STDERR)     at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
W20181216-22:26:04.787(8)? (STDERR) (node:19196) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
W20181216-22:26:04.787(8)? (STDERR) (node:19196) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
W20181216-22:26:04.955(8)? (STDERR) (node:19196) UnhandledPromiseRejectionWarning: RangeError: Maximum call stack size exceeded
W20181216-22:26:04.955(8)? (STDERR)     at Array.forEach (<anonymous>)
W20181216-22:26:04.955(8)? (STDERR)     at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
W20181216-22:26:04.956(8)? (STDERR)     at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
W20181216-22:26:04.956(8)? (STDERR)     at Array.forEach (<anonymous>)
W20181216-22:26:04.956(8)? (STDERR)     at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
W20181216-22:26:04.956(8)? (STDERR)     at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
W20181216-22:26:04.956(8)? (STDERR)     at Array.forEach (<anonymous>)
W20181216-22:26:04.956(8)? (STDERR)     at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
W20181216-22:26:04.957(8)? (STDERR)     at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
W20181216-22:26:04.957(8)? (STDERR)     at Array.forEach (<anonymous>)
W20181216-22:26:04.957(8)? (STDERR)     at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
W20181216-22:26:04.958(8)? (STDERR)     at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
W20181216-22:26:04.958(8)? (STDERR)     at Array.forEach (<anonymous>)
W20181216-22:26:04.959(8)? (STDERR)     at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
W20181216-22:26:04.959(8)? (STDERR)     at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
W20181216-22:26:04.959(8)? (STDERR)     at Array.forEach (<anonymous>)
W20181216-22:26:04.959(8)? (STDERR) (node:19196) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 4)
W20181216-22:26:05.159(8)? (STDERR) (node:19196) UnhandledPromiseRejectionWarning: RangeError: Maximum call stack size exceeded
W20181216-22:26:05.160(8)? (STDERR)     at Array.forEach (<anonymous>)
W20181216-22:26:05.161(8)? (STDERR)     at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
W20181216-22:26:05.161(8)? (STDERR)     at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
W20181216-22:26:05.161(8)? (STDERR)     at Array.forEach (<anonymous>)
W20181216-22:26:05.162(8)? (STDERR)     at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
W20181216-22:26:05.162(8)? (STDERR)     at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
W20181216-22:26:05.163(8)? (STDERR)     at Array.forEach (<anonymous>)
W20181216-22:26:05.163(8)? (STDERR)     at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
W20181216-22:26:05.163(8)? (STDERR)     at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
W20181216-22:26:05.163(8)? (STDERR)     at Array.forEach (<anonymous>)
W20181216-22:26:05.164(8)? (STDERR)     at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
W20181216-22:26:05.164(8)? (STDERR)     at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
W20181216-22:26:05.164(8)? (STDERR)     at Array.forEach (<anonymous>)
W20181216-22:26:05.164(8)? (STDERR)     at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
W20181216-22:26:05.165(8)? (STDERR)     at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
W20181216-22:26:05.165(8)? (STDERR)     at Array.forEach (<anonymous>)
W20181216-22:26:05.166(8)? (STDERR) (node:19196) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 6)

That’s because you are returning a cursor and not what you probably want tot return - an array. To fix your error try this:

let unassignEmployees = Meteor.users.find({
  _id: {
   $nin: assignedEmployeeIdArr
 }
}).fetch();

return unassignEmployees

Notice the fetch method which transforms your cursor into an array.

Also you might want to refactor some bits (just some code tips) to prevent side effects:

  • Use const instead of let wherever you can.
  • Use map in favour of forEach when it comes to mapping arrays. Your example would be:
const assignedEmployeeIds = OutletEmployees.find({}).map({ employee_id } => employee_id);

Hi, correct me if im wrong. Method cannot return cursor?

Nope. Only publications

Ok understand what’s my problem now. Thank you very much.