My node.js, mongoose and MongoDB version.
node: v10.15.0
mongoose: 5.4.1
mongodb: v4.0.4
I am fetching an issue with mongoose UserModel.create() function.
My code snippet is:
req.user = await UserModel.create(user)
when I console req.user
it showed correct object,
{
"_id": "5c47fa1a27b0651dce4da173",
"fullName": "miraje hossain2",
"mobile": "+8801273532927827",
"deviceToken": "sfdlfhdsfdsfdsjflkdsfdsfd",
"userType": 1,
"favoriteSubject": []
}
but when I spread req.user into another object like: let obj = {...req.user}
then obj
returns:
"$__": {
"strictMode": true,
"inserting": true,
"getters": {},
"_id": "5c47fa1a27b0651dce4da173",
"wasPopulated": false,
"activePaths": {
"paths": {
"userType": "require",
"mobile": "require",
"fullName": "require"
},
"states": {
"ignore": {},
"default": {},
"init": {},
"modify": {},
"require": {
"userType": true,
"mobile": true,
"fullName": true
}
},
"stateNames": [
"require",
"modify",
"init",
"default",
"ignore"
]
},
"pathsToScopes": {},
"cachedRequired": {},
"session": null,
"emitter": {
"domain": null,
"_events": {},
"_eventsCount": 2,
"_maxListeners": 0
},
"$options": {}
},
"isNew": false,
"_doc": {
"_id": "5c47fa1a27b0651dce4da173",
"fullName": "miraje hossain2",
"mobile": "+8801273532927827",
"deviceToken": "sfdlfhdsfdsfdsjflkdsfdsfd",
"userType": 1,
"favoriteSubject": []
}
I can't figure out why its happen, can anyone help me to figure it out.
The 2nd (large) object you see is the "real" object.
The "real" mongoose-model-object does not only contain the database-fields but many more meta-fields for internal management.
For example, isNew: false
most likely tells the mongoose-library that if you save()
this object, an UPDATE-type queryis used instead of an INSERT-type query.
Normally you would always see those attributes when logging and doing stuff.
But since this is irritating and changing/depending on those fields on your own can be risky, the mongoose-developers have decided to output only the database-fields (_doc:
) wherever one would assume to see only those fields.
That's the reason why for example JSON.stringify()
the model-object will also only print the _doc
.
If there are any questions left, feel free to ask :)
Mongoose docs have a lot of internal properties for change tracking, etc. Use {...req.user.toObject()}
instead.
@vkarpov15 I tried, but its return ...req.user.toObject() is not a function
@mirajehossain can you check that req.user
is actually instanceof mongoose.Document
? Maybe you made a copy somewhere?
It is too much simple, follow my steps :
Most helpful comment
The 2nd (large) object you see is the "real" object.
The "real" mongoose-model-object does not only contain the database-fields but many more meta-fields for internal management.
For example,
isNew: false
most likely tells the mongoose-library that if yousave()
this object, an UPDATE-type queryis used instead of an INSERT-type query.Normally you would always see those attributes when logging and doing stuff.
But since this is irritating and changing/depending on those fields on your own can be risky, the mongoose-developers have decided to output only the database-fields (
_doc:
) wherever one would assume to see only those fields.That's the reason why for example
JSON.stringify()
the model-object will also only print the_doc
.If there are any questions left, feel free to ask :)