Afsara
Afsara

Reputation: 1461

how to use firebasedatabase transactions?

I want to use firebase database transaction in my react native app. I want to update the users count whenever the user is added in database

here is the code I'm using

let dbCountPath = "/DatabaseUsersCount/" + "count"

    dbCountPath.transaction(function (current_value) {
      return (current_value || 0) + 1;
    });

I'm getting error of transaction is not exists ? how can we solve this issue ?

Upvotes: 0

Views: 372

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599766

You're trying to call transaction on a string. But the transaction method is only defined on a Reference, so you'll first have to get the reference for your path. Something like:

let dbCountPath = "/DatabaseUsersCount/" + "count"

firebase.database().ref(dbCountPath).transaction(function (current_value) {
  return (current_value || 0) + 1;
});

Upvotes: 1

Related Questions