title: How to prevent creating data if other user deleted it's parent recently?
body: <p>I am using firebase's real-time database + Unity, and here is how I created a js function for the voting process. There are a few parts:</p>
  <ol>
  <li>One of the players vote, and it triggers the function newVoteForDetected.</li>
  <li>The second part is checking how many votes this contest has already.</li>
  <li>The function adds votes if it's not enough.</li>
  <li>If the contest has enough votes, the function moves its data to another node for completed contests and finally deletes the data to make only contests without max votes presence in active contests.</li>
  </ol>
  <pre><code>exports.newVoteForDetected = functions.database.ref(&quot;Currentactivecontests/{userid}/Voteforstamp&quot;)
    .onUpdate((snapshot, context) =&gt; {
      var userid = context.params.userid;
      return database.ref(&quot;Currentactivecontests/&quot; + userid + &quot;/for&quot;).once(&quot;value&quot;).then(function(snapshot) {
        var votes = snapshot.val();
        votes = votes + 1;
        if (votes &lt;= 3) {
          return database.ref(&quot;Currentactivecontests/&quot; + userid).update({
            &quot;for&quot;: votes
          });
        } else {
          return database.ref(&quot;Currentactivecontests/&quot; + userid).once(&quot;value&quot;).then(function(snapshot) {
            return database.ref(&quot;Completed&quot;).child(snapshot.key).set(snapshot.val()).then(function() {
             return database.ref(&quot;Currentactivecontests/&quot; + userid).set(null);
            });
          });
        }
      });
      });
  </code></pre>
  <p>It works well, but the problem is sometimes it creates a new &quot;userid&quot; and &quot;for&quot; nodes after deleting by other functions/users. As you can see, I already use update instead of set</p>
  <pre><code>(return database.ref(&quot;Currentactivecontests/&quot;+userid).update({&quot;for&quot;: votes});) 
  </code></pre>
  <p>but it didn't help. When and how can I check if this userid is deleted, so the function shouldn't update this &quot;for&quot; node and not create a new userid node for that? And If I understood correctly, I have to check if it exists <strong>at the same</strong> time that functions try to increment the &quot;for&quot; value - at the same moment, not in the previous function, because other users can complete and delete this contest any second.</p>