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

后端 未结 30 2345
日久生厌
日久生厌 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 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(...).

提交回复
热议问题