用猫鼬填充对象内部的两个对象

时间:2020-07-24 13:07:19

标签: javascript node.js express mongoose mongoose-schema

我有:

const ProfileSchema = new mongoose.Schema({
chats: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Chat'
    } 
  ]
});

我的聊天模式包括:

const ChatSchema = mongoose.Schema({
    messages: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'ChatMsg'
        }
    ],
    members: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'User'
        }
    ]
});

如何填充邮件和成员?目前,我可以填充消息或成员,但不能同时填充两者。这是我当前的代码:

const profile = await Profile.findOne({ user: req.user.id }).populate({
      path: 'chats',
      populate: {
        path: 'members',
        // path: 'messages'
      },
      // populate: {
      //   path: 'messages',
      //   // path: 'members'
      // }
    })
    // .populate({
    //   path:'chats',
    //   populate: {
    //     path: 'members'
    //   }
    // });

编辑:实际上,当我仅填充成员时,它可以工作,并且在message数组中看到数字。但是,当我仅填充消息时,其为空白。我仍然不确定如何解决这个问题。

0 个答案:

没有答案