Example : we are using an object(obj) and it has a key, "fullName".
Now, if we want to alter the "fullName" using the other keys of the same, object, we can use get and set fxns.
If we just access the "fullName", get fxn run
If we change the value, set fxn run automatically.
Q: What is getter and setter functions? How can we use them to define virutals?
Ans: While using an object, we can access the values and change the values.
And to have more functionalities in those terms (more power in our hand), we use getter and setter functions
Arrow functions handle the this keyword much differently than conventional functions.
Mongoose getters/setters depend on this to give you access to the document that you’re writing to,
but this functionality does not work with arrow functions.
we define virutuals while creating the Schemas(Models).
Let's say we have a ProductSchema, we can define the virutals as in image below:
more about get and set functions in later tweet.
peace ✌
Let's talk about VIRTUALS!!!
Mongoose provide a virtual function which allows you to identify a value in DB with some other name.
It is useful when you have a stored value which is named different than what you are using in the Front-end
Let's understand with example :
Mongoose creates a unique id for every inserted data like this :
_id =663fw2e23...
But in frontend we are using the variable "id" not "_id", so we can define a virtual which will,
identify the "_id" as "id" in frontend
@akashmishrahaha That's all good... but I would suggest mentioning the topics that you're learning for the day. Be a little descriptive. And if you think something piqued your interest, write about that too.
Does anyone know what it's saying in a bit detail:
Do not use arrow functions for mongoose getters/setters unless do not intend to access the document in the getter/setter.
@realranament @i_pranavmehta You don't need to watch his videos in order to know what he says in them. I have watched his videos. More or less his content has been same in his videos. And if you find such biased content and still follows that guy, KUDOS TO YOU!