Created
March 26, 2019 18:21
-
-
Save uptownhr/048749c0d85264d502dd9df467dfb1d0 to your computer and use it in GitHub Desktop.
destructuring a mongoose document causes error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const companyUserSchema = new Schema({ | |
profile: { | |
type: profileSchema, | |
default: profileSchema | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require('mongoose') | |
const Schema = mongoose.Schema | |
const emergencySchema = require('./EmergencySchema') | |
function setFirstName (val) { | |
if (val !== null) return val | |
return this.first_name | |
} | |
function setLastName (val) { | |
if (val !== null) return val | |
return this.last_name | |
} | |
function dollarTrim (val){ | |
if (typeof val == "string" && val.trim().includes('$')) return val.trim().substring(1) | |
return val | |
} | |
const profileSchema = new Schema({ | |
first_name: { type: String, trim: true, default: '', set: setFirstName }, | |
last_name: { type: String, trim: true, default: '', set: setLastName }, | |
supervisor: { type: String, trim: true }, | |
role: { type: String, trim: true }, | |
phone: { type: String, trim: true, default: '', set: val => val ? val.replace(/[\s()-]/gi,'') : ''}, | |
type: { type: String, enum: ['', 'hourly', 'salary', 'contractor'], default: ''}, | |
employee_type: { type: String, enum: ['', 'parttime', 'fulltime', 'contractor'], default: ''}, | |
pay_rate: { type: String, trim: true, set: dollarTrim }, | |
pay_day: { type: String, default: '' }, | |
pay_frequency: { type: String, default: '' }, | |
hours_per_week: { type: String, trim :true }, | |
start_date: { type: String, trim: true }, | |
address: { type: String, trim: true }, | |
address2: { type: String, trim: true }, | |
city: { type: String, trim: true }, | |
state: { type: String, trim: true }, | |
zip: { type: String, trim: true }, | |
state_work_in: { type: String, trim: true}, | |
dob: { type: Date, default: null }, | |
lastSocial: { type: String, default: '' }, | |
avatar_url: { type: String, default: '' }, | |
years_experience: { type: String, default: ''}, | |
contractor: { type: Boolean, default: false }, | |
_partner: { type: Schema.Types.ObjectId, ref: 'PartnerUser', default: null }, | |
conference: { type: String, default: ''}, | |
emergency_contact: { | |
type: emergencySchema, | |
default: {} | |
}, | |
timezone: { type: String, default: '' } | |
}, { | |
toJSON: { virtuals: true }, | |
toObject: { virtuals: true }, | |
_id: false, | |
id: false | |
}) | |
profileSchema.virtual('full_name') | |
.get(function () { | |
return (this.first_name + ' ' + this.last_name).trim() | |
}) | |
.set(function (v) { | |
this.first_name = v.substr(0, v.indexOf(' ')) | |
this.last_name = v.substr(v.indexOf(' ') + 1) | |
}) | |
module.exports = profileSchema |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test.only('destructuring mongoose document', t => { | |
const cu = new CompanyUser() | |
const profile = { | |
first_name: 'Tester', | |
last_name: 'Testing', | |
role: 'owner', | |
phone: '818-497-4098', | |
type: 'salary', | |
employee_type: 'fulltime', | |
pay_day: 'Friday', | |
city: 'Test Town' | |
} | |
cu.profile = profile | |
console.log({...cu}) | |
cu.profile = { | |
...cu.profile | |
} | |
console.log(1) | |
console.log(cu) //crashes here with `obj.toObject is not a function` | |
console.log(2) //does not occur | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment