Hey, I'm exploring the world of mongoDB!
Here's the schema I created:
const userSchema = new mongoose.Schema({
firstName:{
type:String,
required:true,
trim:true,
min:3,
max:20
},
lastName:{
type:String,
required:true,
trim:true,
min:3,
max:20
}
})
All good till this point. But now I wanted to add a virtual property, so I tried this out:
//virtual property
userSchema.virtual('fullName').get(()=>{
console.log("first name " + this.firstName);
return this.firstName + " " + this.lastName;
});
However, this
was returning undefined
as it was empty.
Interestingly, when I switched to using the traditional function
keyword, the issue was resolved.
Aren't arrow functions supposed to bind this
? Why did it not work in this case?