微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决我正在使用 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 举报,一经查实,本站将立刻删除。