Can't access object property, even though it shows up in a console log

后端 未结 30 1785
日久生厌
日久生厌 2020-11-22 06:26

Below, you can see the output from these two logs. The first clearly shows the full object with the property I\'m trying to access, but on the very next line of code, I can\

相关标签:
30条回答
  • 2020-11-22 06:56

    For me it turned out to be a Mongoose-related problem.

    I was looping over objects that I got from a Mongo query. I just had to remove:

    items = await Model.find()
    

    And replace it by:

    items = await Model.find().lean()
    
    0 讨论(0)
  • 2020-11-22 06:57

    Check if inside the object there's an array of objects. I had a similar issue with a JSON:

        "terms": {
            "category": [
                {
                    "ID": 4,
                    "name": "Cirugia",
                    "slug": "cirugia",
                    "description": "",
                    "taxonomy": "category",
                    "parent": null,
                    "count": 68,
                    "link": "http://distritocuatro.mx/enarm/category/cirugia/"
                }
            ]
        }
    

    I tried to access the 'name' key from 'category' and I got the undefined error, because I was using:

    var_name = obj_array.terms.category.name
    

    Then I realised it has got square brackets, that means that it has an array of objects inside the category key, because it can have more than one category object. So, in order to get the 'name' key I used this:

    var_name = obj_array.terms.category[0].name
    

    And That does the trick.

    Maybe it's too late for this answer, but I hope someone with the same problem will find this as I did before finding the Solution :)

    0 讨论(0)
  • 2020-11-22 06:57

    I struggled with this issue today, and thought I'll leave a reply with my solution.

    I was fetching a data object via ajax, something like this: {"constants": {"value1":"x","value2":"y"},"i18n" {"data1":"x", "data2":"y"}}

    Let's say this object is in a variable called data. Whenever I referenced data.i18n I got undefined.

    1. console.log(data) showed the object as expected
    2. console.log(Object.keys(data)) said ["constants","i18n"] as expected
    3. Renaming i18n to inter didn't change anything
    4. I even tried to switch the data to make "i18n" the first object
    5. Moved code around to make absolutely sure the object was completely set and there was no problem with the ajax promise.

    Nothing helped... Then on the server side I wrote the data to the php log, and it revealed this:

    {"constants": {"value1":"x","value2":"y"},"\u045618n" {"data1":"x", "data2":"y"}}

    The "i" in the index key was actually a u0456 (cyrillic i). This was not visible in my php editor or the browser console log. Only the php log revealed this... That was a tricky one...

    0 讨论(0)
  • 2020-11-22 06:59

    In 2018 Mozilla warns us in the Mozilla Docs here!

    I quote "Logging Objects":

    Don't use console.log(obj);, use console.log(JSON.parse(JSON.stringify(obj)));.

    This way you are sure you are seeing the value of obj at the moment you log it.

    0 讨论(0)
  • 2020-11-22 07:00

    I've just had this issue with a document loaded from MongoDB using Mongoose.

    When running console.log() on the whole object, all the document fields (as stored in the db) would show up. However some individual property accessors would return undefined, when others (including _id) worked fine.

    Turned out that property accessors only works for those fields specified in my mongoose.Schema(...) definition, whereas console.log() and JSON.stringify() returns all fields stored in the db.

    Solution (if you're using Mongoose): make sure all your db fields are defined in mongoose.Schema(...).

    0 讨论(0)
  • 2020-11-22 07:01

    My data was just json data string . (This variable was stored as json string in the session).

    console.log(json_string_object)
    

    -> returns just the representation of this string and there is no way to make difference whether is string or object.

    So to make it work I just needed to convert it back to real object:

    object = JSON.parse(json_string_object);
    
    0 讨论(0)
提交回复
热议问题