📜  mongoose put request - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:49.752000             🧑  作者: Mango

代码示例1
router.put('/board/:id', (req, res) => {
  const {id: _id} = req.params // Assigning id to _id which is a es6 feature. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
  const {position} = req.body

  const newBoard = {
    _id,
    position
  }

  Board.findByIdAndUpdate(
    _id,
    newBoard,
    (err, updatedBoard) => {
      if (err) {
        res.json({
          newBoard,
          success: false,
          msg: 'Failed to update board'
        })
      } else {
        res.json({newBoard, success: true, msg: 'Board added'})
      }
    }
  )
})