Andre Pavini
Andre Pavini

Reputation: 353

Angular2 update field and sum + 1 (save to firebase)

I´m trying to update a field on my firebase. I´d like to sum 1 to the field and save on firebase

See the code:

 this.chat1
      .update({
        lastMessage: newMessage,
        timestamp: currentTimestamp,
        unreadMessage: unreadMessage + 1 //HERE. Trying to sum the field + 1
      });

What is the best way to do it?

Upvotes: 0

Views: 165

Answers (1)

TheUnreal
TheUnreal

Reputation: 24492

use the firebase transaction method.

as I know this method is not available with angularfire2, so you will have to extract the reference:

this.chat1.$ref
    .ref.transaction(unreadMessage => {
        if (unreadMessages === null) {
            return unreadMessage = 1;
        } else {
            return unreadMessage + 1;
        }
    })

read more about saving date as trasncations here

Upvotes: 1

Related Questions