title: Cloud Functions: Realtime Database transaction not working with condition
body: <p>I am trying to decrease the field <code>amount</code> which represents the amount of products available for sale.</p>
  <p>My database is structured like below:</p>
  <pre><code>- Stock
   - StoreId
    - DepartmentId
     - ProductId
      + price
      + amount
  </code></pre>
  <p>My <code>storeId</code> has a fixed value and I am only looping through the variables <code>DepartmentId</code> and <code>ProductId</code>.</p>
  <p>Below is how I created a node array or children array for the <strong>transaction</strong>:</p>
  <pre><code>  const childArray = [];
    depIds.forEach(depId =&gt; {
      const prodIds = groupedProducts[depId].reduce((acc, val) =&gt; [...acc, val.prodId], []);
      prodIds.forEach(prodId =&gt; {
  
        childArray.push(admin.database().ref(&quot;Stock&quot;)
          .child(storeId)
          .child(depId)
          .child(prodId)
          .child(&quot;amount&quot;)
          .once('value'));
      });
    });
  </code></pre>
  <p>When I add the condition: <code>amount &gt; 0</code>, nothing happens. It only works when I remove the condition.</p>
  <pre><code>const snapshots = await Promise.all(childArray);
  snapshots.forEach(snapshot =&gt; {
    snapshot.ref.transaction(amount =&gt; {
      //if (amount) return amount + admin.database.ServerValue.increment(-1);
      if (amount &gt; 0) return amount - 1;
          
    })
  })
  </code></pre>
  <p><strong>Furthermore, what I really need is to compare the amount in the database with the amount coming from the request (let's call it qty)</strong></p>
  <p>For this, I would have to retrieve the current path for <code>productId</code> and <code>departmentId</code>. But as showed below, snapshot.ref.parent does not return the path.</p>
  <pre><code>snapshots.forEach(snapshot =&gt; {
    snapshot.ref.transaction(amount =&gt; {
      //if (amount &gt; 0 &amp; amount &gt;= qty) return amount + admin.database.ServerValue.increment(-qty);
  
      const productPath = snapshot.ref.parent;
      const departmentPath = snapshot.ref.parent.parent;
      // Get qty based on productPath and departmentPath
      if (amount &gt; 0 &amp; amount &gt;= qty) return amount - qty;
          
    })
  })
  </code></pre>
  <p>So, I have three questions:</p>
  <ol>
  <li>How to retrieve the current paths for departmentId and productId</li>
  <li>How to update the field amount based on a condition and abort the whole transaction if any condition is not satisfied</li>
  <li>How to make admin.database.ServerValue.increment(-qty) work</li>
  </ol>
  <p>I appreciate any help!</p>