title: Firebase .orderByChild().equalTo().once().then() Promises when child doesn't exist
body: <p>My API <code>/auth/login</code> endpoint takes a <code>req.body</code> like this:</p>
  
  <pre><code>{
    "email": "jacob@gmail.com",
    "password": "supersecretpassword"
  }
  </code></pre>
  
  <p>At the endpoint, I make a reference to my Firebase database (<code>https://jacob.firebaseio.com/users</code>). I search through the data, and when I find one user with the email that matches <code>req.body.email</code>, I compare the password with the one stored in the database.</p>
  
  <p>I followed the promise structure outlined <a href="https://firebase.googleblog.com/2016/01/keeping-our-promises-and-callbacks_76.html" rel="noreferrer">in this Firebase blog post</a>.</p>
  
  <pre><code>router.post('/login', function(req, res) {
    const ref = db.ref('/users');
  
    ref.orderByChild('email')
      .equalTo(req.body.email)
      .once('child_added')
      .then(function (snapshot) {
        return snapshot.val();
      })
      .then(function (usr) {
  
        // Do my thing, throw any errors that come up
  
      })
      .catch(function (err) {
  
        // Handle my errors with grace
  
        return;
      });
  });
  </code></pre>
  
  <p>If no child is found at <code>ref</code>, the function does not proceed (see <a href="https://stackoverflow.com/questions/34078755/firebase-orderbychild-equalto-hangs-when-no-data">this answer</a>). I don't even get an error thrown.</p>
  
  <p>My goal is to run code when no user is found with a certain email (i.e. no child is found at <code>ref</code> that satisfies <code>.equalTo(req.body.email)</code>), but not run the code if a user is found. No error is thrown when nothing is found.</p>
  
  <p>I tried adding <code>return</code> statements at strategic points in the call to the database (at the end of my <code>.then()</code> promises) with the intention of breaking out of the endpoint entirely after the code was run. I then put code after the call to the database:</p>
  
  <pre><code>    .then(function (usr) {
  
        // Do my thing, throw any errors that come up
  
      })
      .catch(function (err) {
  
        // Handle my errors with grace
  
        return;
      });
  
    res.status(401)
      .json({
        error: 'No user found',
      )};
  
    return;
  });
  </code></pre>
  
  <p>but this code is run whether the call to the database succeeds or not, since the call is asynchronous.</p>
  
  <p>How do I react to this call to the database if it doesn't return anything <em>and</em> still use Firebase promises?</p>