Scrolling Mongoose Objects
I have a Mongoose result object that I would like to skip, but it seems to behave differently than the classic object.
Generally, you cannot loop through uncountable properties with for…in loop
, but that seems to work here. Hence, this does not work with Object.keys
, although it is assumed to fulfill the same properties as for…in
...
Also Object.getOwnPropertyNames(obj)
, which must intersect all properties (enumerable or not) returns none of the selected values. I really don't understand what's going on here.
Here's some sample code and output to illustrate this:
MediaModel.findById('558d0b3a5fa02e7e218b470c', function (err, media) {
if (err) {
return console.error(err);
}
console.log(media);
console.log("\n --- \n");
console.log(Object.getOwnPropertyNames(media));
console.log("\n---\n");
for (var prop in media) {
console.log(prop + ' -- Enumerable : ' + media.propertyIsEnumerable(prop) + ' -- hasOwnProperty : ' + media.hasOwnProperty(prop) + "\n");
}
});
It outputs this to the console:
{ _id: 558d0b3a5fa02e7e218b470c,
name: '54ae5d587860b4.61272171',
cover: true,
size: 144,
audio:
{ es: '54ae5ec1e31c68.83020900',
en: '54ae5ec240dd98.65329366',
fr: '54ae5ec2950737.15028179',
pt: '54ae5ec2e3bcf5.08479198',
ar: '54ae5ec33ab8f5.67860635',
ru: '54ae5ec393d300.66214842' },
comments: [] }
---
[ '$__',
'isNew',
'errors',
'_doc',
'$__original_save',
'save',
'_pres',
'_posts' ]
---
$__ -- Enumerable : true -- hasOwnProperty : true
isNew -- Enumerable : true -- hasOwnProperty : true
errors -- Enumerable : true -- hasOwnProperty : true
_doc -- Enumerable : true -- hasOwnProperty : true
$__original_save -- Enumerable : true -- hasOwnProperty : true
save -- Enumerable : true -- hasOwnProperty : true
_pres -- Enumerable : true -- hasOwnProperty : true
_posts -- Enumerable : true -- hasOwnProperty : true
db -- Enumerable : false -- hasOwnProperty : false
discriminators -- Enumerable : false -- hasOwnProperty : false
__v -- Enumerable : false -- hasOwnProperty : false
id -- Enumerable : false -- hasOwnProperty : false
_id -- Enumerable : false -- hasOwnProperty : false
comments -- Enumerable : false -- hasOwnProperty : false
size -- Enumerable : false -- hasOwnProperty : false
length -- Enumerable : false -- hasOwnProperty : false
cover -- Enumerable : false -- hasOwnProperty : false
imdbId -- Enumerable : false -- hasOwnProperty : false
imdbRating -- Enumerable : false -- hasOwnProperty : false
year -- Enumerable : false -- hasOwnProperty : false
actors -- Enumerable : false -- hasOwnProperty : false
writers -- Enumerable : false -- hasOwnProperty : false
director -- Enumerable : false -- hasOwnProperty : false
genre -- Enumerable : false -- hasOwnProperty : false
name -- Enumerable : false -- hasOwnProperty : false
schema -- Enumerable : false -- hasOwnProperty : false
collection -- Enumerable : false -- hasOwnProperty : false
$__handleSave -- Enumerable : false -- hasOwnProperty : false
$__delta -- Enumerable : false -- hasOwnProperty : false
$__version -- Enumerable : false -- hasOwnProperty : false
increment -- Enumerable : false -- hasOwnProperty : false
$__where -- Enumerable : false -- hasOwnProperty : false
remove -- Enumerable : false -- hasOwnProperty : false
model -- Enumerable : false -- hasOwnProperty : false
on -- Enumerable : false -- hasOwnProperty : false
once -- Enumerable : false -- hasOwnProperty : false
emit -- Enumerable : false -- hasOwnProperty : false
listeners -- Enumerable : false -- hasOwnProperty : false
removeListener -- Enumerable : false -- hasOwnProperty : false
setMaxListeners -- Enumerable : false -- hasOwnProperty : false
removeAllListeners -- Enumerable : false -- hasOwnProperty : false
addListener -- Enumerable : false -- hasOwnProperty : false
$__buildDoc -- Enumerable : false -- hasOwnProperty : false
init -- Enumerable : false -- hasOwnProperty : false
$__storeShard -- Enumerable : false -- hasOwnProperty : false
hook -- Enumerable : false -- hasOwnProperty : false
pre -- Enumerable : false -- hasOwnProperty : false
post -- Enumerable : false -- hasOwnProperty : false
removePre -- Enumerable : false -- hasOwnProperty : false
_lazySetupHooks -- Enumerable : false -- hasOwnProperty : false
update -- Enumerable : false -- hasOwnProperty : false
set -- Enumerable : false -- hasOwnProperty : false
$__shouldModify -- Enumerable : false -- hasOwnProperty : false
$__set -- Enumerable : false -- hasOwnProperty : false
getValue -- Enumerable : false -- hasOwnProperty : false
setValue -- Enumerable : false -- hasOwnProperty : false
get -- Enumerable : false -- hasOwnProperty : false
$__path -- Enumerable : false -- hasOwnProperty : false
markModified -- Enumerable : false -- hasOwnProperty : false
modifiedPaths -- Enumerable : false -- hasOwnProperty : false
isModified -- Enumerable : false -- hasOwnProperty : false
isDirectModified -- Enumerable : false -- hasOwnProperty : false
isInit -- Enumerable : false -- hasOwnProperty : false
isSelected -- Enumerable : false -- hasOwnProperty : false
validate -- Enumerable : false -- hasOwnProperty : false
validateSync -- Enumerable : false -- hasOwnProperty : false
invalidate -- Enumerable : false -- hasOwnProperty : false
$markValid -- Enumerable : false -- hasOwnProperty : false
$isValid -- Enumerable : false -- hasOwnProperty : false
$__reset -- Enumerable : false -- hasOwnProperty : false
$__dirty -- Enumerable : false -- hasOwnProperty : false
$__setSchema -- Enumerable : false -- hasOwnProperty : false
$__getArrayPathsToValidate -- Enumerable : false -- hasOwnProperty : false
$__getAllSubdocs -- Enumerable : false -- hasOwnProperty : false
$__registerHooksFromSchema -- Enumerable : false -- hasOwnProperty : false
$__handleReject -- Enumerable : false -- hasOwnProperty : false
$toObject -- Enumerable : false -- hasOwnProperty : false
toObject -- Enumerable : false -- hasOwnProperty : false
toJSON -- Enumerable : false -- hasOwnProperty : false
inspect -- Enumerable : false -- hasOwnProperty : false
toString -- Enumerable : false -- hasOwnProperty : false
equals -- Enumerable : false -- hasOwnProperty : false
populate -- Enumerable : false -- hasOwnProperty : false
execPopulate -- Enumerable : false -- hasOwnProperty : false
populated -- Enumerable : false -- hasOwnProperty : false
$__fullPath -- Enumerable : false -- hasOwnProperty : false
In the end, I would like to go through media.audio
, but cannot figure out how.
It works by executing var audio = (JSON.parse(JSON.stringify(media.audio)));
and then iterating over it with for…in
, but I don't believe this is the correct way to do it!
source to share
I think you can do it with
var media = {
_id: '558d0b3a5fa02e7e218b470c',
name: '54ae5d587860b4.61272171',
cover: true,
size: 144,
audio: {
es: '54ae5ec1e31c68.83020900',
en: '54ae5ec240dd98.65329366',
fr: '54ae5ec2950737.15028179',
pt: '54ae5ec2e3bcf5.08479198',
ar: '54ae5ec33ab8f5.67860635',
ru: '54ae5ec393d300.66214842'
},
comments: []
};
for (var property in media.audio) {
if (media.audio.hasOwnProperty(property)) {
console.log(property);
}
}
source to share