嗨,我正在尝试将用户填充到另一个名为feedbackschema的模式中。
反馈模式
const mongoose = require('mongoose');
const {
Schema,
} = mongoose;
// Create Schema
const FeedbackSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'users',
},
pro: {
type: String,
required: true,
},
con: {
type: String,
required: true,
},
comments: {
type: String,
required: true,
},
rating: {
type: String,
required: true,
},
});
// Create model
const feedback = mongoose.model('feedbacks', FeedbackSchema);
module.exports = feedback;
用户模式
const mongoose = require('mongoose');
const {
Schema,
} = mongoose;
// Create Schema
const UserSchema = new Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
lowercase: true,
},
password: {
type: String,
required: true,
},
isAdmin: {
type: Boolean,
required: true,
default: false,
},
});
// Create a model
const user = mongoose.model('users', UserSchema);
// Export the model
module.exports = user;
这是我的控制器,我试图在这里填充用户
getAllFeedbacks: async (req, res) => {
const errors = {};
try {
const feedbacks = await Feedback.find().populate('user');
return res.json(feedbacks);
} catch (err) {
errors.noFeedbacks = 'Please try again';
return res.status(404).json(errors);
}
},
我通过邮递员收到的Json是这个吗
[
{
"_id": "5b3adf88f3c4cd836bdc2eda",
"pro": "knfklngfdklgnfdgknkln",
"con": "Sales executive updates",
"comments": "This is a another funfact for me is me too",
"rating": "8",
"__v": 0
}
]
它应该显示用户密钥,但不知何故它不起作用。我检查了当前用户数据是否已经存在,但由于某些原因,它没有推送用户信息反馈对象。
反馈收集
[
{
"_id": {
"$oid": "5b3adf88f3c4cd836bdc2eda"
},
"pro": "knfklngfdklgnfdgknkln",
"con": "Sales executive updates",
"comments": "This is a another funfact for me is me too",
"rating": "8",
"__v": 0
}
]
用户集合
[
{
"_id": {
"$oid": "5b37e456565971258da97d5e"
},
"isAdmin": false,
"name": "montygoldy",
"email": "montygoldy@gmail.com",
"password": "$2a$10$zWbxV0Q3VPUxRC6lzJyPBec3P/8zYBaSCTJ2n88Uru3zzFlicR2rq",
"__v": 0
}
]