您好,他们的我正在try 填充postedby字段创建一个新的 comments . 当我创建一条 comments 时,我将它保存在MongoDB上,还将Comment._id保存在我的特性模型上(作为ObjectID) 然后我发送res.json( comments ) 我可以在发送json响应之前填充吗? 我也试过了,但什么也没发生 我的密码是-
exports.createComment = async (req, res) => {
console.log("run?");
const { featureId } = req.params;
const { content } = req.body;
const postedBy = req.auth._id;
if (!content) {
return res
.status(400)
.send({ error: "Please provide a content with your comment." });
}
const comment = new Comment({
content,
postedBy,
});
await comment.save();
await comment.populate("postedBy", "_id username");
Feature.findByIdAndUpdate(
featureId,
{ $push: { comments: comment._id } },
{ new: true }
).exec((err, result) => {
if (err) {
return res.json({
error: errorHandler(err),
});
}
});
return res.json(comment);
};```