Object.keys()从集合中返回MongoDB对象上的意外键 [英] Object.keys() returns unexpected keys on MongoDB object from collection

查看:58
本文介绍了Object.keys()从集合中返回MongoDB对象上的意外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里处理一个奇怪的问题.这是从mongodb中提取并传递给以下函数的对象数组.

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

我在从数据库中拉出的阵列的forEach中依次尝试了以下3个日志:

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

  • e(数组中的对象元素)可以正确返回.如您所见,所有属性(键)都存在:
  • 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))返回的不是键...
    • console.log(Object.keys(e)) is returning things that are not the keys...
    • [ '__parentArray',
        '__parent',
        '__index',
        '$__',
        'isNew',
        'errors',
        '_doc',
        '$init' ]
      

      • 最后:
      • 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);
                    }
                }
        ....
        

        我需要访问属性(键)以在forEach函数中进行进一步处理.我对造成这种情况的原因非常困惑,并且从未遇到过此类问题.为了记录,对象存在,使用console.log(typeof e)表示对象是对象(而不是数据字符串").我可以使用点或括号符号来访问属性,但不能使用Object.keys()for (keys in obj).

        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?

        推荐答案

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

        对于Object.keys(),除非它由不兼容的实现覆盖,否则实际上它们是对象本身的可枚举键,因此您很可能会误解. e对象具有 .toJSON() 方法在其原型中会在执行console.log(e)时隐式调用,因此可能是您在此处看到的输出,并且不太可能反映出与原始对象完全相同的属性键.尝试调用console.log(e.toJSON()),我想它的输出将与第一个相同.

        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).

        如果要在第一个输出中打印键,请使用Object.keys(e.toJSON()).

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

        这篇关于Object.keys()从集合中返回MongoDB对象上的意外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆