我正在使用 mongoose populate 从 2 个文档中的 mongodb 中获取数据,但结果没有来自引用文档的数据

问题描述

这两个模型是用户和个人资料

const userSchema = new mongoose.Schema({
  email: String,password: String,googleId: String,profile: {
    type: mongoose.Schema.Types.ObjectId,ref: "Profile"
  }
});
const User = mongoose.model("User",userSchema);

const profileSchema = new mongoose.Schema({
  firstName: String,lastName: String,workEmail: String,dateOfBirth: Date,location: String,employee: {
    type: mongoose.Schema.Types.ObjectId,ref: "User"
  }
})
const Profile = mongoose.model("Profile",profileSchema);

我想使用 populate 链接用户和个人资料,以便我可以获得当前在会话中的用户的个人资料 (req.user.id),但我的 populate 函数只返回没有个人资料详细信息的用户 这是我的获取路线

app.get("/employeeprofile",function (req,res) {
    User.find({_id : req.user.id}).populate('profile')
    .exec(function (err,user) {
      if (err) {
        console.log(err);
      }
      if(user){
        console.log(user);
        return user;
      }
  })
  })

用户在没有引用的配置文件的情况下登录控制台。我该如何解决这个问题,以便将用户配置文件作为一个文档返回(不嵌入,仅使用引用)并且不会出现未定义的错误

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)