'Access document with dynamic keys returned by findOne mongoose API

I am trying to access the nested object dynamically, in the document returned by findOne. But I am receiving this error:

    console.log(doc.$[category])
                         ^

TypeError: Cannot read properties of undefined (reading 'chats')
    at file:///C:/Users/lenovo/Desktop/commdash/backend/server.js:93:26      
    at processTicksAndRejections (node:internal/process/task_queues:96:5)  

My code for findOne is:

mongoData.findOne( {_id: id,  [category] :{$elemMatch:{name: name}}}).then(function (doc) {
    console.log(doc.$[category])
});

My Schema is:

{
user : {
        firstName: String,
        lastName: String,
        email: String,
        userImage: {type: String, default: "https://www.business2community.com/wp-content/uploads/2017/08/blank-profile-picture-973460_640.png"}
    },
    avatar: {type: String, default: "https://www.business2community.com/wp-content/uploads/2017/08/blank-profile-picture-973460_640.png"},
    chats: [{
                name: String, 
                conversation: [
                    {
                        message: String,
                        timestamp: String,
                        user: String,
                        userImage: {type: String, default: avatar}
                    }
                ]
            }],
    groups:[ {
        name: String,
        conversation: [
            {
                message: String,
                timestamp: String,
                user: String,
                userImage: {type: String, default: avatar}
            }
        ]
    }],
    people: [{
        id: String,
        name: String,
        relation : [{name: String}],
        conversation: [
            {
                message: String,
                timestamp: String,
                user: String,
                userImage: {type: String, default: avatar}
            }
        ]
    }
    ]
}

}

The dynamic variable category is either: people, groups, or chats.

When its only console.log(doc), it returns:

{
  user: {
    userImage: 'https://www.business2community.com/wp-content/uploads/2017/08/blank-profile-picture-973460_640.png',
    firstName: 'xyz',
    lastName: 'xyz',
    email: '[email protected]'
  },
  _id: new ObjectId(""),
  avatar: 'https://www.business2community.com/wp-content/uploads/2017/08/blank-profile-picture-973460_640.png',
  chats: [
    {
      _id: new ObjectId(""),
      name: 'Hello',
      conversation: [Array]
    },
    {
      _id: new ObjectId(""),
      name: 'There',
      conversation: [Array]
    },
    {
      _id: new ObjectId(""),
      name: 'up',
      conversation: [Array]
    }
  ],
  groups: [],
  people: [],
  __v: 0
}

and when I try console.log(doc.chats), it returns:

[
  {
    _id: new ObjectId(""),
    name: 'Hello',
    conversation: [ [Object] ]
  },
  {
    _id: new ObjectId(""),
    name: 'There',
    conversation: [ [Object] ]
  },
  {
    _id: new ObjectId(""),
    name: 'up',
    conversation: [ [Object] ]
  }
]

This is again wrong because what is wanted is a particular chats object with chats.name = name? I don't know what I am doing wrong.Please help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source