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

后端 未结 30 1784
日久生厌
日久生厌 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:52

    I had the same issue today. Problem was caused by uglify-js. After I executed same non-uglified code problem was solved. Removing of

    --mangle-props
    

    from uglify-js was enough to have working uglified code.

    Perhaps, the best practice is to use some prefix for properties that has to be mangled with regex rule for uglify-js.

    Here is the source:

    var data = JSON.parse( content);
    ...
    this.pageIndex = parseInt(data.index);
    this.pageTotal = parseInt(data.total);
    this.pageLimit = parseInt(data.limit); 
    

    and here is how it was uglified:

    var n = JSON.parse( t);
    ...
    this._ = parseInt(n.index), this.g = parseInt(n.total), this.D = parseInt(n.C)
    
    0 讨论(0)
  • 2020-11-22 06:53

    If this is an issue occurring when working with Mongoose, the following may happen:

    console.log(object)
    

    returns everything, including the desired key.

    console.log(object.key)
    

    returns undefined.

    If that is happening, it means that the key is missing from the Mongoose Schema. Adding it in will resolve the issue.

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

    I just encountered this issue as well, and long story short my API was returning a string type and not JSON. So it looked exactly the same when you printed it to the log however whenever I tried to access the properties it gave me an undefined error.

    API Code:

         var response = JsonConvert.DeserializeObject<StatusResult>(string Of object);
         return Json(response);
    

    previously I was just returning:

    return Json(string Of object);
    
    0 讨论(0)
  • 2020-11-22 06:54

    I had the same issue. Solution for me was using the stringified output as input to parsing the JSON. this worked for me. hope its useful to you

    var x =JSON.parse(JSON.stringify(obj));
    console.log(x.property_actually_now_defined);
    
    0 讨论(0)
  • 2020-11-22 06:55

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

    Turned out that i'm using the property find() to return just one object, so i changed find() to findOne() and everything worked for me.

    Solution (if you're using Mongoose): Make sure to return one object only, so you can parse its object.id or it will be treated as an array so you need to acces it like that object[0].id.

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

    if you're using TYPESCRIPT and/or ANGULAR, it could be this!

    .then((res: any) => res.json())

    setting the response type to any fixed this issue for me, I couldn't access properties on the response until i set res: any

    see this question Property '_body' does not exist on type 'Response'

    0 讨论(0)
提交回复
热议问题