这些是我从获取请求到配置文件API的user
响应
"user": "5cc3a4e8d37a7259b45c97fe"
我正在寻找的是
"user":{
"_id": "5cc3a4e8d37a7259b45c97fe",
"name":"Jhon Doe",
}
这是我的代码:
Profile.findOne({
user: req.user.id
})
.populate('user',['name']) // I added this line to populate user object with name
.then(profile=>{
if(!profile){
errors.noprofile = 'There is no profile for this user'
return res.status(404).json(errors);
}
res.json(profile)
})
.catch(err => res.status(404).json(err));
但是,我收到了以下错误消息:
{
"message": "Schema hasn't been registered for model \"users\".\nUse mongoose.model(name, schema)",
"name": "MissingSchemaError"
}
我想念什么
配置文件架构
const ProfileSchema = new Schema({
user:{
type: Schema.Types.ObjectId,
ref: 'users'
},
handle: {
type: String,
required: true,
max: 40
},
company: {
type: String
},
website: {
type: String,
}
})
这是我的用户架构的样子
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// Create Schema
const UserSchema = new Schema({
name:{
type: String,
required: true,
},
email:{
type: String,
required: true,
},
password:{
type: String,
required: true,
},
avator:{
type: String,
},
date:{
type: Date,
default: Date.now,
}
});
module.exports = User = mongoose.model('Users', UserSchema)
答案 0 :(得分:0)
该错误表明您没有用于用户的架构。您从Profile Schema引用了它,但是没有它。可以这样:
const Users = new Schema({
name: String
})
答案 1 :(得分:0)
您在Profile
模式中引用的模式为users
,但是您已将用户模式另存为Users
。所以我想说,您需要更新Profile
模式:
const ProfileSchema = new Schema({
user:{
type: Schema.Types.ObjectId,
ref: 'Users'
},
handle: {
type: String,
required: true,
max: 40
},
company: {
type: String
},
website: {
type: String,
}
})
在此行中可以找到User
模式的保存名称
module.exports = User = mongoose.model('Users', UserSchema)