Skip to content

Instantly share code, notes, and snippets.

@wkentdag
Created October 14, 2015 22:38
Show Gist options
  • Save wkentdag/3945d43066a681e8a638 to your computer and use it in GitHub Desktop.
Save wkentdag/3945d43066a681e8a638 to your computer and use it in GitHub Desktop.
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
Company = mongoose.model('Company'),
Loan = mongoose.model('Loan');
var ParticipationSchema = new Schema({
participationPercentage: {
type: Number,
required: 'Please specify a participation percentage.'
},
lender: {
type: Schema.ObjectId,
ref: 'Company',
required: 'Please specify a lender associated with this loan.'
},
loan: {
type: Schema.ObjectId,
ref: 'Loan',
required: 'Please specify a loan.'
},
managementFee: {
type: Number,
required: 'Please specify a management fee for this participation.'
},
created: {
type: Date,
default: Date.now
}
});
ParticipationSchema.virtual('info').get(function() {
// return {foo: 'bar', baz: 'foo'};
// ^ works as expected
var _this = this;
var info = {};
Company.findById(_this.lender).exec(function(err, company) {
Loan.findById(_this.loan).exec(function(err, loan) {
if (!loan && company) return info;
info.loanName = loan.name;
info.lenderName = company.name;
info.balance = loan.principalBalance * _this.participationPercentage;
info.originationFee = loan.originationFee;
info.interestRate = loan.interestRate;
info.loanStatus = loan.status;
console.log(info); // <-- spits out all the correct info
return info; // <-- nothing shows up
}); // end Loan.findById
}); // end Company.findById
});
ParticipationSchema.set('toJSON', {virtuals: true});
ParticipationSchema.set('toObject', {virtuals: true});
mongoose.model('Participation', ParticipationSchema);
module.exports = ParticipationSchema;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment