想實現的是reply - save
後,同時操作topic
表和user
表,然後一起返回,不知怎麼用promise.all
來包裝這2個查詢操作,希望大家能幫我解下= =
更新:下面是修改好的.
/* 回复 话题 */
router.post('/reply', (req, res, next) => {
let topic_id = req.body.topic_id,
content = req.body.content
let replyEntity = new replyModel({
author: req._id,
topic: topic_id,
content
})
replyEntity.save()
.then((_new_reply) => {
Promise.all([
topicModel.findByIdAndUpdate(topic_id, {
$inc: {replyNum: 1},
last_reply_author: req._id,
last_reply_time: Date.now()
}),
userModel.findByIdAndUpdate(req._id, {
$push: {replies: _new_reply._id}
})
])
.then((res_arr) => {
return res.json({
status: 0
})
})
.catch((err) => {
return res.json({
status: -1
})
})
})
.catch((err) => {
return res.json({
status: -1
})
})
})
大概寫了下,你試試看呢