Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object.keys() returns unexpected keys on MongoDB object from collection [duplicate]

Working with a strange problem here. This is an array of objects which is pulled from mongodb and passed into the following function.

I tried the following 3 logs sequentially within the forEach on the array pulled from the database:

  • e (the object element within the array) which returns correctly. as you can see all the properties (keys) exist:
{ paid: false,   
  hotelWebsite: 'www.testing.com',   
  _id:5951848a24bb261eed09d638,   
  hotelAddress: '123 easy street',
...etc }
  • console.log(Object.keys(e)) is returning things that are not the keys...
[ '__parentArray',
  '__parent',
  '__index',
  '$__',
  'isNew',
  'errors',
  '_doc',
  '$init' ]
  • and finally:
for(key in e){
    console.log(key);
}

which returns an absolute mess of data, part of which DOES contain the actual keys of the object:

__parentArray
__parent
__index
$__
isNew
errors
_doc
$init
id
_id
hotelWebsite
hotelAddress
hotelNumber
hotelName
courseCost
courseDate
courseState
courseCity
courseName
paid
studentComments
studentEmail
studentPhone
studentCountry
studentZip
studentState
studentCity
studentAddress
studentCompany
studentName
schema
constructor
$__original_remove
remove
_pres
_posts
$__original_validate
validate
toBSON
markModified
populate
save
update
inspect
invalidate
$markValid
$isValid
ownerDocument
$__fullPath
parent
parentArray
on
once
emit
listeners
removeListener
setMaxListeners
removeAllListeners
addListener
$__buildDoc
init
$hook
$pre
$post
removePre
removePost
_lazySetupHooks
set
$__shouldModify
$__set
getValue
setValue
get
$__path
unmarkModified
$ignore
modifiedPaths
isModified
$isDefault
isDirectModified
isInit
isSelected
isDirectSelected
$__validate
validateSync
$__reset
$__dirty
$__setSchema
$__getArrayPathsToValidate
$__getAllSubdocs
$__handleReject
$toObject
toObject
toJSON
toString
equals
execPopulate
populated
depopulate

And a relevant sample of the code if needed:

studentsArray.forEach( (e, i) => {

        if(task === 'nameTag'){
            console.log(e);
            console.log(Object.keys(e));
            for(k in e){
                console.log(k);
            }
        }
....

I need access to the properties (keys) for further processing within the forEach function. I am very confused on what is causing this and have never run into this sort of issue before. For the record the objects exist, using a console.log(typeof e) it IS an object (not a data "string"). I can access the properties using the dot or bracket notation but NOT using Object.keys() or for (keys in obj).

Can anyone help me sort this out please?

like image 336
vampiire Avatar asked Jun 29 '17 19:06

vampiire


1 Answers

for ... in iterates all enumerable properties, both own and inherited. This is not "a strange bug," this is in fact the intended behavior.

As for the Object.keys(), unless it was overwritten by a non-compliant implementation, those are in fact enumerable keys of the object itself, so you are most likely mistaken. The e object has a .toJSON() method in its prototype that is implicitly called when you do console.log(e), so that is probably the output you are seeing there, and is not likely going to reflect exactly the same property keys as the original object. Try calling console.log(e.toJSON()) and I'm guessing it will be the same output as in the first one.

If you want only the object's own properties, use Object.getOwnPropertyNames(e).

If you want the keys printed in the first output, then use Object.keys(e.toJSON()).

like image 73
Patrick Roberts Avatar answered Nov 05 '22 00:11

Patrick Roberts