title: How to push in firebase cloud function transaction?
body: <p>I have the following struct:</p>
  
  <pre><code>{
    "point_logs": {
      "user1": {
        "LrDm-0OBBg84rTSXGyF": {
          "action": "action_type_1",
          "point": 1
        },
        "LrDm0b0oF-48EF3ZsqF": {
          "action": "action_type_2",
          "point": 1
        }
      },
      "user2": {
        "LrDm0dfZsEE40HvnwEc": {
          "action": "action_type_5",
          "point": 1
        },
        "LrDm0gKsdEw3O3ync_7": {
          "action": "redeem_1",
          "point": -2
        }
      }
    }
  }
  </code></pre>
  
  <p>I am currently add a firebase cloud function to redeem point, since the cloud function is <code>async</code>, in order to make sure there is sufficient point to redeem, I need make redeem function atomic. </p>
  
  <p>I tried use transaction:</p>
  
  <pre><code>exports.redeemPoints = functions.https.onRequest((req, res) =&gt; {
    db.ref('/point_logs/' + user_id).transaction((data) =&gt; {
      admin.database().ref('/points_assign_logs/' + user_id).once('value', (snapshot) =&gt; {
        // code to iterate and sum all points from this user's logs
        if (remain_points &gt;= redeem_point) {
          admin.database().ref('/point_logs/' + user_id).push(redeem_log);
        }
        return data
      });
    });
  });
  </code></pre>
  
  <p>But with multiple async call to this function, the remain point can be negative, even though there is a check <code>if (remain_points &gt;= redeem_point)</code>.</p>
  
  <p>How can I user transaction correctly to atomic this log update?</p>