title: Update Mongoose: Help on how to update document in mongoose
body: <p>I am newbie and working for my thesis.
  I was able to create insert and delete function but getting issues updating the data.</p>
  <p>The below is the code under the controllers folder:</p>
  <p>Insert Document:</p>
  <pre><code>module.exports.register = (params) =&gt; {
      let user = new User({
          firstName: params.firstName,
          lastName: params.lastName,
          department: params.department,
          position: params.position,
          email: params.email,
          mobileNo: params.mobileNo,
          password: bcrypt.hashSync(params.password, 10),
          isAdmin: params.isAdmin,
          departments: {
              departmentId: params.departmentId
          }
      })
  
      return user.save().then((user, err) =&gt; {
          return (err) ? false : true
      })
  }
  </code></pre>
  <p>Delete Document:</p>
  <pre><code>module.exports.deleteUser = (params) =&gt; {
      return User.findByIdAndRemove(params.userId).then((user, err) =&gt; {
          return (err) ? false : true
      })
  }
  </code></pre>
  <p>While the below is the code for the routers:</p>
  <p>Insert Document:</p>
  <pre><code>router.post('/register', (req, res) =&gt; {
      UserController.register(req.body).then(result =&gt; res.send(result))
  });
  </code></pre>
  <p>Delete Document:</p>
  <pre><code>router.delete('/delete/:id', (req, res) =&gt; {
      let userId = req.params.id
      UserController.deleteUser({userId}).then(user =&gt; res.send(user))
  });
  </code></pre>
  <p>What I've tried to far is to use the delete logic but instead of findOneAndRemove, I used findOneAndUpdate but it's not updating the data. It just sending true value but not updating the document.</p>
  <p>I've also tried some of the logic in YouTube and some here but it doesn't matched the way we construct the data so I'm having difficulties understanding them.</p>
  <p>The target is to update the same values in the register using the id of the user.</p>
  <p>I'm trying to update using this code:</p>
  <p>Controllers:</p>
  <pre><code>module.exports.updateUser = (params) =&gt; {
      return User.findOneAndUpdate(params.userId, (user, err) =&gt; {
          return (err) ? false : true
      })
  }
  </code></pre>
  <p>Routes:</p>
  <pre><code>router.post('/update/:id', (req, res) =&gt; {
      UserController.updateUser(req.params.id).then(user =&gt; res.send(user))
  });
  </code></pre>
  <p>I've also tried to add the parameters but it's not working. What I want to update is the whole details example:</p>
  <pre><code>        firstName: params.firstName,
          lastName: params.lastName,
          department: params.department,
          position: params.position,
          email: params.email,
          mobileNo: params.mobileNo,
          password: bcrypt.hashSync(params.password, 10),
          isAdmin: params.isAdmin,
          departments: {
              departmentId: params.departmentId
          }
  </code></pre>
  <p>Sample User:</p>
  <pre><code>{
      &quot;userId&quot;: &quot;60f649bd8896c80004b3ffbe&quot;,
      &quot;firstName&quot;: &quot;Jane&quot;,
      &quot;lastName&quot;: &quot;Joe&quot;,
      &quot;department&quot;: &quot;Accounting&quot;,
      &quot;position&quot;: &quot;head&quot;,
      &quot;email&quot;: &quot;janedoe@mail.com&quot;,
      &quot;mobileNo&quot;: &quot;0&quot;,
      &quot;password&quot;: &quot;pass123&quot;,
      &quot;isAdmin&quot;: &quot;yes&quot;,
      &quot;departments&quot;: {
          &quot;departmentId&quot;: &quot;60efcbec769cf60004b85319&quot;
      }
  }
  </code></pre>
  <p>Here's my update code:
  Controller:</p>
  <pre><code>module.exports.updateUserData = (params) =&gt; {
      const dataToUpdate = {
          firstName: params.firstName,
              lastName: params.lastName,
              department: params.department,
              position: params.position,
              email: params.email,
              mobileNo: params.mobileNo,
              password: bcrypt.hash(params.password, 10),
              isAdmin: params.isAdmin,
              departments: {
                  departmentId: params.departmentId
              }
         }
      
      User.findOneAndUpdate({userId:params.userId}, {$set:dataToUpdate}, {new: true}, (err, doc) =&gt; {
          if (err) {
              console.log(&quot;Something wrong when updating data!&quot;);
          }
          console.log(doc);
      });
  }
  </code></pre>
  <p>Routers:</p>
  <pre><code>router.post('/update-user/:id', (req, res) =&gt; {
      let userId = req.params.id;
      UserController.updateUserData({userId}).then(doc =&gt; res.send(doc))
  })
  </code></pre>
  <p>Using this code I am getting error message: TypeError: Cannot read property 'then' of undefined under routers folder.</p>