22 ~ express ~ 内容评论实现

1,使用 ajax 提交评论内容 给 api.js 

2,数据库 contents 增加评论字段

3,后台路由 api.js 接收并完成存储

/** 增加评论(用户,内容,时间) */
router.post('/comment/post',(req,res)=>{
/**
* 需要前端提交文章的id
* 接收判断是哪一个文章的评论
*/
var contentId = req.body.contentId

var postData = {
username : req.userInfo.username,
postTime : new Data(),
content: req.body.content
}

/**查询当前这篇文章的信息 */
Content.findOne({_id:contentId}).then((content)=>{
 
/** 将post数组中有关评论的信息 存入文章 */
content.comments.push(postData)
/** 数据库保存 */
return content.save()
}).then(newContent=>{ //新的内容
/** 通过 json 返回前端数据 */
responseData.message = '评论成功'
res.responseData(responseData)
})
})

猜你喜欢

转载自www.cnblogs.com/500m/p/11070837.html
22