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

virtual path on array #2326

Open
cncolder opened this issue Sep 26, 2014 · 4 comments
Open

virtual path on array #2326

cncolder opened this issue Sep 26, 2014 · 4 comments
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature

Comments

@cncolder
Copy link

in mongose guild doc there is a cool example. define .full virtual on sub documents name: {first:String, last:String}. then get virtual value by name.full.

but if i define virtual on array like this:

var schema = new Schema({
  nums: [Number]
});
schema.virtual('nums.odd').get(...).

i cannot get it by dot path nums.odd. only write like this model.get('nums.odd').

is there a way to fix this?
i'm running mongoose@3.9.3

@vkarpov15 vkarpov15 added this to the 3.8.18 milestone Sep 26, 2014
@vkarpov15
Copy link
Collaborator

That's weird, that should work in theory. I'll investigate.

@cncolder
Copy link
Author

maybe my fault.
only object path can define virtual append with dot.
i don't know how mongoose do that. define a real property on that object? or intercept invoke on schema level?

here is some test code:

  describe('gh-2306', function () {
    it('allow define virtual on non-object path', function () {
      var db = start();
      var schema = new mongoose.Schema({ num: Number, str: String, nums: [Number] });
      schema.virtual('num.power').get(function () {
        return this.num * this.num;
      });
      schema.virtual('str.upper').get(function () {
        return this.str.toUpperCase();
      });
      schema.virtual('nums.last').get(function () {
        return this.nums[this.nums.length - 1];
      });
      var M = db.model('gh2306', schema);
      var m = new M({ num: 2, str: 'a', nums: [1,2,3] });

      assert.equal(m.num.power, 4);
      assert.equal(m.str.upper, 'A');
      assert.equal(m.nums.last, 3);
    });
  });

@shrayolacrayon
Copy link
Contributor

There is no good way to support this right now because getters and setters do not go through the . operator and virtuals are defined in the document level.

You can access the virtuals in the test above as m.get('num.power').

@shrayolacrayon shrayolacrayon removed this from the 3.8.19 milestone Nov 6, 2014
@shrayolacrayon shrayolacrayon added the enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature label Nov 6, 2014
@danielmahon
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Projects
None yet
Development

No branches or pull requests

4 participants