What we want to achieve isreply - save
After that, operate at the same time.topic
Tables anduser
Table, and then return together, I do not know how to usepromise.all
To package these 2 query update operations, I hope you can help me solve the = =
Update: The following is a revised version.
/* Reply to Topics */
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
})
})
})
replyEntity.save() .then((_new_reply) => { var topic = function (topic_id,authorId) { return topicModel.findByIdAndUpdate(topic_id, { $inc: {replyNum: 1}, last_reply_author: authorId, last_reply_time: Date.now() }).exec(); } var user = function (authorId,replyId) { return userModel.findByIdAndUpdate(authorId, { $push: {replies: replyId} }).exec(); } return Promise.all([topic(topic_id,req._id), user(req._id,_new_reply._id)]) .then(function (results) { console.log('===results===',results); return res.json({ status: 0 }) }).catch((err) => { return res.json({ status: -1 }); }).catch((err) => { return res.json({ status: -1 }) })
Probably wrote it down, would you like to try it?