title: using fetch API with NodeJS(express)
body: <p>I am making an app and adding functionality to it that checks in real-time if a username is already taken or not.</p>
  <p><em><strong>Stack info</strong></em>: MongoDB, NodeJS(Express), VanillaJS for the front end.</p>
  <p>I am using fetch API on the client-side and promises on the server-side.</p>
  <p><em><strong>What am I doing here:</strong></em></p>
  <p>I am taking the value from the input element and then making an AJAX call to a route that checks the database for the value. If the database returns some data then it means that the username is already taken if no data is returned then it is available for use.</p>
  <p><em><strong>The Error message which I am Getting is ==&gt;</strong></em></p>
  <pre><code>(node:1697) UnhandledPromiseRejectionWarning: username already in use
  (node:1697) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, 
  or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
  (node:1697) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
  </code></pre>
  <p><em><strong>This is the route and associated function:</strong></em></p>
  <pre><code>app.get(&quot;/api/check/:username&quot;, (req,res)=&gt;{
      checkDuplicate(req,res);
  });
  function checkDuplicate(req, res) {
      return new Promise(function(resolve, reject){
          User.findOne({username: req.params.username}, function(err, data){
              //no data found, username availabe
              if(!data){
                  resolve();
              //database error
              }else if(err){
                  reject(&quot;some error occurred&quot;);
              //data found, user already exist
              }else{
                  reject(&quot;username already taken&quot;);
              }
          });
      });
  }
  </code></pre>
  <p><em><strong>this is the front end javascript code:</strong></em></p>
  <pre><code>fetch(`/api/check/${username.value}`)
              .then(showMessage(&quot;username Availabe, you are ready to go&quot;))
              .catch(err =&gt; showMessage(err, true));
  </code></pre>
  <p>PS: this is the first time that I am using AJAX calls like this and also I am quite new to promises and probably don't completely understand them.</p>