试图填充每个聊天室的每个所有者

trying to populate each owner of each chatrooms

每个聊天室由两个所有者组成,一个用户自己和另一方。 我正在尝试提取对方的用户名,以便我可以将其与聊天室 ID

一起发送

我试图填充所有者,以便我可以提取对方的详细信息。填充不工作

  const chatRoomSchema = new mongoose.Schema({ 
   owners:[{
        type:mongoose.Schema.Types.ObjectId,
        required:true,
        ref:'User'
   }]

   },{
      timestamps:true  
       })

    //virtuals to relate chatroom with messages
       chatRoomSchema.virtual('chatter', {
             ref:'message',
               localField : '_id',
              foreignField:'room'
             })

        chatRoomSchema.set('toObject', { virtuals: true });
         chatRoomSchema.set('toJSON', { virtuals: true });

       const ChatRoom = mongoose.model('chatroom',chatRoomSchema)

//获取聊天室对方详情的路由

        router.get('/chatrooms',auth, async (req, res) => {

       const user = req.user

     try {

    //Getting all chatrooms
    const chatroom = await ChatRoom.find({owners:{$all:[user._id]}})


    const chatrooms = await chatroom.populate('owners').execPopulate()
    res.send(chatrooms) 

    } catch (error) {
    res.status(500).send(error)
 }

 })

    const chatroom = await ChatRoom.find({owners:{$all:[user._id]}})

给出以下结果

    [
       {
       owners: [
          "5d6caefdbb6f2921f45caf1d",
          "5d6caee9bb6f2921f45caf1b"
         ],
        _id: "5d6ccf5d55b38522a042dbb2",
          createdAt: "2019-09-02T08:14:21.734Z",
         updatedAt: "2019-09-02T08:14:21.734Z",
        __v: 0,
       id: "5d6ccf5d55b38522a042dbb2"
        },
       {
           owners: [
       "5d6dfcd6e3b11807944348b8",
       "5d6caefdbb6f2921f45caf1d"
                      ],
      _id: "5d6dfd48e3b11807944348ba",
          createdAt: "2019-09-03T05:42:32.572Z",
          updatedAt: "2019-09-03T05:42:32.572Z",
         __v: 0,
      id: "5d6dfd48e3b11807944348ba"
      }
      ]

但填充给出 {}

我想做的是提取每个聊天室中的对方并将他们发回给客户端。采取了以下步骤

1) 填充聊天室的所有者

  const chatrooms = await ChatRoom.find({owners:{$all:[user._id]}}).populate('owners','-password').exec()

2) 将所有所有者提取到一个数组中

  const allowners = chatrooms.flatMap(room => room.owners) 

3) 提取每个聊天室中除用户以外的所有所有者

  //declared userid = user._id
  const vendors = allowners.filter(item => !item.equals(userid))