Firdous Alam
Firdous Alam

Reputation: 603

how to implement virtual along with async loop

I am using async loop to loop through the given result, when I am doing that I am unable to get the virtual key "name" in the iteration.

Any solution is welcome, I am stuck with this issue.

var result_subscriptions_of_user = _.where(data.subscription_details, {'user_id': subscription_detail.user_id});

// value are coming in result_subscriptions_of_user

// Iterate through the user subscription details

async.eachSeries(result_subscriptions_of_user, function iteratee(subscription_user_detail, callback) { //here subscription_user_detail is not comming

what wrong i am doing here

my schema is var subscription_schema = new db.schema({

subscription_id : Number,

created_on : { type: Date, default: Date.now },

created_by : object_id,

provisioned_on : Date,

expiry_date : Date,

status : String,

}, {toObject: {virtuals: true,getters:true },toJSON: {virtuals: true,getters:true }});

and my virtual function is

subscription_schema.virtual('actual_expiry_date').get(function () 
{ 
    var moment          = require('moment-timezone');
    var date            = moment(this.expiry_date).format('DD-MM-YYYY'); 
    return date;  
})

Upvotes: 0

Views: 54

Answers (1)

KibGzr
KibGzr

Reputation: 2093

Note that if the resulting record is converted to an object or JSON, virtuals are not included by default. Pass virtuals : true to either toObject() or to toJSON() to have them returned. You need set userSchema like this

userSchema.set('toObject', { virtuals: true })

check this document

Upvotes: 0

Related Questions