Navigation Menu

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

Reference virtual in virtual? #3423

Closed
gumpcha opened this issue Oct 1, 2015 · 4 comments
Closed

Reference virtual in virtual? #3423

gumpcha opened this issue Oct 1, 2015 · 4 comments
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.

Comments

@gumpcha
Copy link

gumpcha commented Oct 1, 2015

Is it possible referencing virtual attribute in other virtual attribute?

Here is what I tried and user.editLink was <a href="/users/some_user_id">undefined</a>.

UserSchema.virtual('fullName').get(function() {
    return this.firstName + ' ' + this.lastName;
});

UserSchema.virtual('editLink').get(function() {
    return '<a href="/users/' + this.id + '">' + this.fullName + '</a>';
});

I'm using mongodb 2.4.17 and mongoose 4.1.6.

@vkarpov15
Copy link
Collaborator

Should be possible. Is this code not working for you?

@gumpcha
Copy link
Author

gumpcha commented Oct 2, 2015

this.fullName in editLink is undefined.

above code is not working for me. :(

Did I miss something?

@vkarpov15
Copy link
Collaborator

The following script executes without error for me:

'use strict';

var assert = require('assert');

var mongoose = require('mongoose');
var ObjectId = mongoose.Types.ObjectId;
mongoose.set('debug', true);
mongoose.connect('mongodb://localhost:27017/gh3423');

var userSchema = new mongoose.Schema({
  firstName: String,
  lastName: String
});

userSchema.virtual('fullName').get(function() {
  return this.firstName + ' ' + this.lastName;
});

userSchema.virtual('link').get(function() {
  return 'test:' + this.fullName;
});

var User = mongoose.model('User', userSchema);

var user = new User({ firstName: 'Axl', lastName: 'Rose' });

assert.equal(user.link, 'test:Axl Rose');
process.exit(0);

Please modify it to reproduce your issue.

@vkarpov15 vkarpov15 added this to the 4.1.10 milestone Oct 2, 2015
@vkarpov15 vkarpov15 added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Oct 2, 2015
@vkarpov15 vkarpov15 modified the milestones: 4.1.11, 4.1.10 Oct 5, 2015
@vkarpov15 vkarpov15 modified the milestones: 4.2.1, 4.1.11 Oct 12, 2015
@gumpcha
Copy link
Author

gumpcha commented Oct 15, 2015

It worked now. I can't reproduce it anymore. :(

Thank you for kind example anyway.

@gumpcha gumpcha closed this as completed Oct 15, 2015
@vkarpov15 vkarpov15 removed this from the 4.2.1 milestone Oct 15, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
Projects
None yet
Development

No branches or pull requests

2 participants