[英]Mongoose Object returns undefined when populating
I'm struggling with a little kinda of problem.我正在努力解决一些问题。 What I wanna do is populating users in comments.
我想做的是在评论中填充用户。
User schema:用户架构:
const userSchema = mongoose.Schema({
username: {
type: String,
required: true,
},
password: {
type: String,
required: true
}
});
Comment schema:评论模式:
const commentSchema = mongoose.Schema({
comment:{
type: String
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
});
I had already created user and comment.我已经创建了用户和评论。 Everything is fine when I'm trying to find both objects.
当我试图找到这两个对象时,一切都很好。
Comment:评论:
Comment.find({}).exec((err, comments) => {
console.log(comments);
});
Output:输出:
[
{
_id: 5e62472d5f593f3c642ee1e5,
comment: 'something',
user: 5e624522366d8c4150278a64,
__v: 0
}
]
User:用户:
User.find({}).exec((err, users) => {
console.log(users);
});
Output:输出:
[
{
_id: 5e624522366d8c4150278a64,
username: "SomeBodY",
password: "$2a$10$nm5BJ7zeI1tet3UEzcakf.8xoTgV/Yti5l1EKNg5inxiehevUlGRm"
}
]
The problem is when I'm using .populate('user')
to Comment
model it returns the comment as undefined
in the console.问题是当我使用
.populate('user')
来Comment
模型时,它在控制台中返回undefined
的评论。 I've tried different methods and even dropping the database but without success.我尝试了不同的方法,甚至删除了数据库但没有成功。
Here's the route when this happens这是发生这种情况时的路线
// Route To Single Project
router.get('/:projectId', (req, res) => {
const requestedProjectId = req.params.projectId;
Project.findById({_id: requestedProjectId}).populate('image_file').exec((err, project) => {
Rating.find({projectId: requestedProjectId}, (err, ratings) => {
Rating.countDocuments({projectId: requestedProjectId}, (err, count) => {
Comment.find({projectId: requestedProjectId}).populate('user').exec((err, comments) => {
console.log(comments)
if(err) return next(err);
res.render('project', { ... });
});
});
});
});
});
Actually your populate code is true.实际上,您的填充代码是正确的。
The reason to get empty comments is because this Comment.find({projectId: requestedProjectId})
seems to return empty.得到空评论的原因是因为这个
Comment.find({projectId: requestedProjectId})
似乎返回空。 So just check your request param.所以只需检查您的请求参数。
Also to get rid of callback hell, you can rewrite your route using async/await like this.同样为了摆脱回调地狱,你可以像这样使用 async/await 重写你的路由。
router.get("/:projectId", async (req, res) => {
const requestedProjectId = req.params.projectId;
try {
const project = await Project.findById({ _id: requestedProjectId }).populate("image_file");
if (!project) {
return res.status(400).send("Project not found, check your projectId");
}
const comments = await Comment.find({ projectId: requestedProjectId }).populate("user");
console.log(comments);
const ratings = await Rating.find({ projectId: requestedProjectId });
const count = await Rating.countDocuments({ projectId: requestedProjectId });
res.render("project", {
project,
comments,
ratings,
count
});
} catch (err) {
console.log("Error: ", err);
res.status(500).send("Something went wrong");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.