首页 > 解决方案 > MongoDB,NodeJs - 引用模式时遇到问题

问题描述

我有两个架构,一个是用户架构,另一个是配置文件架构,我想将我的配置文件架构指向用户架构,但问题是当我填充配置文件架构时,api 返回一个空数组

用户模式

const mongoose=require("mongoose");
const schema=mongoose.Schema;
const RegisterUser = schema(
{
    username :{type : String , required : true , unique : true},
    email : {type : String, required : true,},
    password : {type : String,required : true},
    followers : [{type : schema.Types.ObjectId, ref : 'Follower'}],
    following : [{type : schema.Types.ObjectId,ref : 'Following'}],
    profile_ref : [{type : schema.Types.ObjectId, ref : 'Profile'}] 
    }); 
   module.exports = mongoose.model("User",RegisterUser);

配置文件架构-

const mongoose=require("mongoose")
const Schema = mongoose.Schema;
const profile=Schema({

username :{type : String,required : true,unique : true},
name :{type : String,required : true},
bio :{type : String},
about:{type : String},
profile_pic : {type : String,default: ""},
phone : {type : String},
 });
 module.exports = mongoose.model("Profile",profile);

这是我填充个人资料文档的路线-

router.route("/:_id/getInfo").get((req,res)=>{

User.findOne({_id : req.params._id}).populate('profile_ref').select('name').exec((err,data)=>{
    if(err)
    {
        res.json({err : err})
    }
    res.json(data);

})})

我从端点得到的响应是:

{
  "profile_ref": [],
  "_id": "60f58798aebdf33f5862b681"

}

标签: node.jsapirest

解决方案


推荐阅读