title: See if a user exists at a firebase location
body: <p>What is a way to check if a user exists at a firebase location?  Would you use <code>.child()</code> somehow? A variable would store the user name entered:</p>
  
  <p>user name:</p>
  
  <pre><code>var user = "test";
  </code></pre>
  
  <p>firebase returned object:</p>
  
  <pre><code>snapshot { '
     -JruhOP3M496hEAKn6': 
     { 
        email: 'chaselester.cl@gmail.com',
        password: 'kimberlyn#1',
        username: 'chipe' 
     },
    '-Ju8J72xo3UpSIwEYw': { 
        email: 'test', 
        password: 'test', 
        username: 'test' },
    '-Ju8J9m_DC18irjWep': { 
        email: 'one', 
        password: 'three', 
        username: 'two' 
    } 
  }
  </code></pre>
  
  <h2>Update: code from comment</h2>
  
  <p>It seems if the user does not exist <code>firebaseRef.push</code> calls <code>firebaseRef.orderByChild</code> and the whole function runs again because in the log it logs "User does not exist" then right after it logs "User Exists" and my res.json is called twice and I get an app crash with the comment "Can't set headers after they are sent". How can I push to firebase if the user does not exist without having it re evaluate the <code>firebaseRef.orderByChild</code>?</p>
  
  <p>Here is the full function I am doing in my server.js: </p>
  
  <pre><code>firebaseRef.orderByChild('username').equalTo(user).on('value', function(snapshot) { 
    if (snapshot.hasChildren()) { 
      console.log('User exists'); 
      res.json(true); 
    }else if(!snapshot.hasChildren()){ 
      console.log('User does not exist'); 
      firebaseRef.push(req.body); 
      res.json(false); 
    } 
  }); 
  </code></pre>