title: TypeError: User.findByIdAndUpdate is not a function
body: <p><strong>Answer</strong>
  this fixed: <code>mongoose.model('User').findByIdAndUpdate(...</code> although it doesn't seem right. Should be another way to require this. Will figure out.</p>
  
  <p>I have a signup form, where user can check a role to either create a candidate or an employer account. Let's say, user picked employer, then mongoose post hook will be triggered to create an employer inside employer collection and return employer._id so that I can save it to user collection as reference. In the last code snippet, <code>findByIdAndUpdate</code> is not triggered and failed silently. Log, outputs only <code>a</code>.</p>
  
  <p>user.js (user model)</p>
  
  <pre><code>const mongoose = require('mongoose');
  const { Schema } = mongoose;
  const bcrypt = require('bcrypt-nodejs');
  
  const {
      updateUserWithEmployerId,
      updateUserWithCandidateId
  } = require('../api/user');
  
  const { createCandidate } = require('../api/candidate');
  const { createEmployer } = require('../api/employer');
  
  const userSchema = new Schema({
      firstName: String,
      lastName: String,
      email: {
          type: String,
          unique: true,
          lowercase: true
      },
      password: String,
      role: {
          type: String,
          enum: ['candidate', 'employer']
      },
      _employerId: {
          type: Schema.Types.ObjectId,
          ref: 'Employer'
      },
      _candidateId: {
          type: Schema.Types.ObjectId,
          ref: 'Candidate'
      }
  }, {
      timestamps: true
  });
  
  userSchema.post('save', function(doc, next){
      if(doc.role === 'employer'){
          return  createEmployer(doc._id)
                  .then(response =&gt; updateUserWithEmployerId(doc._id, response.employer._id))
                  // .catch(error =&gt; next(error))
                  // .catch(error =&gt; response.status(500).send(error))
      }else if(doc.role === 'candidate'){
          return  createCandidate(doc._id)
                  .then(response =&gt; updateUserWithCandidateId(doc._id, response.candidate._id))
                  .catch(error =&gt; response.status(500).send(error))
      }else{
          throw new Error('User role not found!');
      }
      next();
  });
  
  const ModelClass = mongoose.model('User', userSchema);
  module.exports = ModelClass;
  </code></pre>
  
  <p>employer.js(this is not model)</p>
  
  <pre><code>const Employer = require('../models/employer');
  
  exports.createEmployer = (userId) =&gt; {
      return new Promise(function(resolve, reject){
          const employerInstance = new Employer({ _userId: userId });
  
          if(employerInstance){
              employerInstance.save((err, employer) =&gt; {
                  if(err){
                      reject({ message: "Error occured while creating employer!" });
                  }else{
                      resolve({ employer: employer });
                  }
              });
          }else{
              reject({ message: "Error occured while creating employer!" });
          }
      });
  };
  </code></pre>
  
  <p>user.js (not a model)</p>
  
  <pre><code>const User = require('../models/user');
  
  exports.updateUserWithEmployerId = (userId, employerId) =&gt; {
      return new Promise(function(resolve, reject){
          console.log("a");
          User.findByIdAndUpdate(userId, { _employerId: employerId }, { new: true },
          (err, user) =&gt; {
              console.log("user:", user);
              if(err){
                  console.log("b");
                  reject({ message: "Error occured while updating user with employer id!" });
              }else{
                  console.log("c");
                  resolve({ user: user });
              }
          });
          console.log("d");
      });
  };
  </code></pre>