Mongodb 未读标志未使用Mongoose更新

Mongodb 未读标志未使用Mongoose更新,mongodb,express,mongoose,Mongodb,Express,Mongoose,我的数据库中有一个conversations集合,我正在使用Mongoose更新单个文档的unread标志 这是我的代码: router.post('/reply/:conversation_id', ensureAuthenticated, (req, res, next) => { Conversation.findById(req.params.conversation_id, (err, conversation) => { // If the user tha

我的数据库中有一个
conversations
集合,我正在使用Mongoose更新单个文档的
unread
标志

这是我的代码:

router.post('/reply/:conversation_id', ensureAuthenticated, (req, res, next) => {
  Conversation.findById(req.params.conversation_id, (err, conversation) => {

    // If the user that's logged in was the one who created the conversation, and is submitting a reply, run this code
    if (req.user._id == conversation.created_by_user_id) {
      User.findById(conversation.sent_to_user_id, (err, user) => {
        Message.create({
          //...
        }, (err, message) => {
          if (err) {
            console.log(err)
          } else {
            message.conversations.push(conversation._id)
            conversation.unread = true
            conversation.save()  // This is being saved to the database
            message.save()
            res.redirect('/conversations/' + conversation._id)
          }
        })
      })
    } else {
    // Otherwise, if the user that's logged in was *not* the one who created the conversation, and is submitting a reply, run this code

      User.findById(conversation.created_by_user_id, (err, user) => {
        Message.create({
          //...
        }, (err, message) => {
          if (err) {
            console.log(err)
          } else {
            message.conversations.push(conversation._id)
            conversation.unread = true
            conversation.save() // This is not being saved
            message.save()
            res.redirect('/conversations/' + conversation._id)
          }
        })
      })
    }
  })
});
if
部分将
conversation.unread=true
保存到数据库。
else
部件不可用

条件的两个部分基本上做相同的事情(将对话的
未读
标记保存为
,并保存消息),但只有条件的第一部分在将
未读
设置为真时起作用


有人能帮我弄清楚为什么
未读
标记没有在
其他
语句中保存为

您正在尝试同步调用
保存

.save
接受回调。它是异步的

请参阅下面的我的版本

router.post('/reply/:conversation_id', ensureAuthenticated, (req, res, next) => {
  Conversation.findById(req.params.conversation_id, (err, conversation) => {

    // If the user that's logged in was the one who created the conversation, and is submitting a reply, run this code
    if (req.user._id == conversation.created_by_user_id) {
      User.findById(conversation.sent_to_user_id, (err, user) => {
        Message.create({
          //...
        }, (err, message) => {
          if (err) {
            console.log(err)
          } else {
            message.conversations.push(conversation._id)
            conversation.unread = true
            conversation.save()  // This is being saved to the database
            message.save()
            res.redirect('/conversations/' + conversation._id)
          }
        })
      })
    } else {
    // Otherwise, if the user that's logged in was *not* the one who created the conversation, and is submitting a reply, run this code

    User.findById(conversation.created_by_user_id, (err, user) => {
      Message.create({
          //...
        }, (err, message) => {
          if (err) {
            console.log(err)

            //return here
            return res.redirect('/error'); //

          } else {
            message.conversations.push(conversation._id)
            conversation.unread = true

            //.save takes a function callback
            conversation.save((err) => {

              //.save takes a function callback
              message.save((err) => {
                res.redirect('/conversations/' + conversation._id)
              })
            })
          }
        })
    })
  }
})
});