title: firebase check if child exists
body: <p>So I have my backend using firebase. What I'm aiming to do is to add user matches to a user id. However, when the user initially signs up, they have no matches. What I'm trying to do is to check whether or not the "match" child exists within a user child, if not a list child is created and the first match is stored. However, if it already exists, the match is simply added. Here's my code:</p>
  
  <pre><code>    public void setMatch(final String match){
      final Firebase ref = new Firebase("FIREBASEURL");
      final Firebase userRef = ref.child("Flights").child(userName);
      userRef.addValueEventListener(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
              System.out.println("does the child exist? " + dataSnapshot.child("matches").exists());
              if(!dataSnapshot.child("matches").exists()){
                  ArrayList&lt;String&gt; matches = new ArrayList&lt;String&gt;();
                  matches.add(match);
                  Firebase matchesRef = userRef.child("matches");
                  matchesRef.setValue(matches);
                  userRef.removeEventListener(this);
              }else if(dataSnapshot.child("matches").exists()){
                  Map&lt;String, Object&gt; matches = new HashMap&lt;&gt;();
                  matches.put("matches", match);
                  userRef.child("matches").push().setValue(matches);
                  userRef.removeEventListener(this);
              }
          }
  
          @Override
          public void onCancelled(FirebaseError firebaseError) {
  
          }
      });
  }
  </code></pre>
  
  <p>Currently, the value is being added twice (the else if is called twice if the field already exists/its called if it doesn't). I'm not sure what I'm doing wrong.</p>