在我的MEAN堆栈的应用程序,我已经2个集 - 新闻和评论。当评论发布,它将首先被保存,然后它会更新新闻采集。 我使用的是2个功能与快递的帮助下,如下:
//CommentsController.js
//Function 1
exports.addComment = (req, res, next) => {
const comment = new Comment({
//Key values
});
comment
.save()
.then(createdComment => {
req.createdComment = comment;
next();
})
};
//Function 2
exports.updateNews = (req, res, next) => {
let newsId = req.body.newsId;
let comment = req.createdComment;
News.findById(newsId)
.then(news => {
News.update({ _id: newsId }, {
$push: { comment }
})
.then(item => {
res.status(201).json({
message: "Comment added successfully"
});
})
});
}
我的路由器文件如下:
const express = require("express");
var CommentsController = require('../controllers/commentsController');
const router = express.Router();
router.post("", CommentsController.addComment);
module.exports = router;
我现在面临的问题是,addComment保存成功后,它不叫“updateNews”,即使下一个()被调用的函数。我不知道什么是失踪。
AS通过@vapurrmaid(在评论)提到,我找到了答案,以我自己的问题。 答案是:
router.post("", CommentsController.addComment, CommentsController.updateNews);
由于@vapurrmaid