node.js - 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"
}
解决方案
推荐阅读
- docker - Docker运行,没有响应
- javascript - JQuery 动画设置超时
- ruby-on-rails - Google::Apis::AuthorizationError(未授权)
- python - 为什么我的简单 Spark 应用程序运行如此缓慢?
- javascript - 将 org.mozilla.javascript.NativeDate 插入 FreeMarker
- hta - 如何开始隐藏元素,然后在从 HTA 下拉列表中选择它时显示它?
- ruby-on-rails - 需要/从单独的文件扩展 ClassMethod
- matlab - 将matlab中地图上的点与地球曲率连接起来
- angularjs - Mocha 5.1.1 未测试所有文件
- angular - 错误类型错误:无法读取未定义的属性“news_category”