Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongoose objects cause infinite recursion in clone method #19

Open
sansmischevia opened this issue Aug 7, 2012 · 1 comment
Open

Mongoose objects cause infinite recursion in clone method #19

sansmischevia opened this issue Aug 7, 2012 · 1 comment

Comments

@sansmischevia
Copy link

This deep clone method may cause infinite recursion on Mongoose objects in version 2.7.1 of mongoose when trying to log the mongoose object as meta. Maybe prevent this by adding a maximum depth to this function?

common.clone = function (obj, lvl) {
  lvl = lvl || 1;
  var clone = {};
  if (lvl > MAX_DEPTH) {
    return clone;
  }

  for (var i in obj) {
    clone[i] = obj[i] instanceof Object ? common.clone(obj[i], lvl+1) : obj[i];
  }

  return clone;
};
@mattcasey
Copy link

this one's very annoying, however.. a 'fix' for apps using the log function is to call .toObject() on the mongoose instance. that would also work inside of here.. if toObject exists, call that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants