Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7846967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:45:35+00:00 2026-06-02T17:45:35+00:00

I defined: var PersonSchema = new Schema({ avatar: Number }); PersonSchema.virtual(avatarUrl).get(function() { return http://example.com/avatar/

  • 0

I defined:

var PersonSchema = new Schema({
    avatar: Number
});
PersonSchema.virtual("avatarUrl").get(function() {
    return "http://example.com/avatar/" + this.avatar;
});

and this works in node’s code likes:

PersonModel.findOne(function(err, person) {
    console.log(person.avatarUrl); // http://example.com/avatar/1
});

but in jade template, if I use:

img(src!= person.avatarUrl)

this would not give what I expect.

Is there any way to make mongoose virtual works in jade?

EDIT

I think the problem is not thing to do with jade,
coz I save the “person” object in session, before I save it to session, everything on “virtual” works fine, but not works when I get it from session again.
may be it’s something about the session mechanism.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T17:45:36+00:00Added an answer on June 2, 2026 at 5:45 pm

    not sure what ‘session mechanism’ you are using but if its something like connect-redis then its calling JSON.stringify(req.session) before storing. this transforms a mongoose document into a static javascript object with no getters/setters/virtuals or other dynamic abilities. if you want the current values of your virtuals stored in the session as static values as well, you might want to call req.session.person = doc.toJSON({ virtuals: true }).

    another option is to reify the session person into a MongooseDocument after it is returned from the session.

    // assume the session has been retrieved already
    var person = new Person;
    person.init(req.session.person);
    req.session.person = person;
    console.log(req.session.person.someVirtual); // yay!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I constructed a function defined as var Func1 = function() { return { alert:
I have an image in my code, as defined here var imageObj = new
I've got an array of objects defined like this: var editionsObj = new Array();
I defined two functions for array: Array.prototype.remove = function(obj) { var i = this.length;
Let us presume I have the following object defined: var myObj = function(){ this.hello
If I have the following anonymous type List defined: var list = new[] {
I have a panel defined like this: var msgDetailsPanel = new Ext.ux.ManagedIFrame.Panel({ title: 'M',
I have a Javascript class defined like so: var Welcomer = function(name) { var
If yes is there any way to access a var defined in another PHP
Possible Duplicate: C# 'var' keyword versus explicitly defined variables EDIT: For those who are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.