title: Mongoose sometimes skips adding data to DB when using Update method inside of Promise function
body: <p>I'm writing an API using Node.js Express framework, which makes requests to some other external APIs. I need to have logs of each request in database (I use MongoDB via mongoose).</p>
  
  <p>The problem is that when I'm trying to push log to array of sub documents by mongoose Update method, sometimes it saves in database and sometimes not, in both cases without any error.
  Here's some parts of executing code:</p>
  
  <p><strong>// externalApiCtrl module</strong></p>
  
  <pre><code>const request = require('request');
  const mongoose = require('mongoose');
  const httpContext = require('express-http-context');
  const PolicyLog = mongoose.model('PolicyLog');
  
  const updatePolicyLog = (id, log) =&gt; {
    return new Promise((resolve, reject) =&gt; {
      PolicyLog.update({ "policyId": mongoose.Types.ObjectId(id) },
        { $push: { logs: log } }
      ).then(() =&gt; {
        resolve();
      }).catch(err =&gt; {
        reject(err);
      });
    });
  };
  
  exports.createPolicy = (policy) =&gt; {
    return new Promise((resolve, reject) =&gt; {
      // prepare request body and do some other stuff here
  
      let options = {}; // request options (url, method, headers &amp; body)
      request(options, (error, response, body) =&gt; {
        if (error)
          reject(error);
  
        let policyLocalId = httpContext.get("policyLocalId");
        // here comes the trouble
        updatePolicyLog(policyLocalId, {
          method: "reqName",
          request: "reqBody",
          response: body
        }).then(() =&gt; {
          resolve();
        }).catch(err =&gt; {
          return reject(err)
        });
      });
  
    });
  };
  </code></pre>
  
  <p><strong>// Main controller module</strong></p>
  
  <pre><code>exports.create = (req, res) =&gt; {
    let externalApiCtrl = require('./controllers/external-api.controller.js');
    externalApiCtrl.createPolicy(req.policy)
      .then(result =&gt; {
        return res.json(result);
      }).catch(err =&gt; {
      return res.status(501).json(err);
    });
  };
  </code></pre>
  
  <p>So when updatePolicyLog function is called, looks like sometimes it resolves before mongoose Update method (PolicyLog.update()) is resolved.
  Is there any way of solving such issue? </p>