Skip to content

4.0.0-rc2: Setter not called on array of refs #2698

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

Closed
brandom opened this issue Feb 19, 2015 · 0 comments · Fixed by #2699
Closed

4.0.0-rc2: Setter not called on array of refs #2698

brandom opened this issue Feb 19, 2015 · 0 comments · Fixed by #2699
Milestone

Comments

@brandom
Copy link
Contributor

brandom commented Feb 19, 2015

I have a depopulate plugin so that I can call .set on a document with a populated object and avoid casting errors.

depopulate: function(schema, options) {
  schema.eachPath(function(path, type) {
    if (path !== '_id' && type.options.type === Schema.Types.ObjectId || type.caster && type.caster.instance === 'ObjectID') {
      schema.path(path).set(function(val) {
        if (_.isArray(val) && val[0] && val[0]._id) {
          return _.pluck(val, '_id');
        }
        return (_.isObject(val) && val._id) ? val._id : val || undefined;
      });
    }
  });
}

This code works as expected on 3.8.x but fails for arrays of refs on 4.0.0-rc2 resulting in a caster validation error before the setter is reached.

I believe I have traced the issue to this commit: 9ba0fb5 which applies caster setters for each object in an array before performing the setter on the array itself. I am not familiar enough with the code to know whether or not this was intentional, however it does break the above plugin.

I fixed this issue by removing the early return and placing the array caster setters after the path setters. I will submit a PR, but this may need input from the devs/original commiter regarding the implications of changing the order of operations here.

@vkarpov15 vkarpov15 added this to the 4.0.0-rc3 milestone Feb 19, 2015
vkarpov15 added a commit that referenced this issue Feb 19, 2015
@paglias paglias mentioned this issue Oct 19, 2015
28 tasks
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

Successfully merging a pull request may close this issue.

2 participants