WJA
WJA

Reputation: 7004

Firebase v3 transactions and promises

I am struggling a bit with how to handle transactions in Firebase v3.

I tried the following:

function fbTransaction(childRef) {
    var qTrans = Q.defer();

    firebase.database().ref(childRef).transaction()
    .then(function(success){
        qTrans.resolve(success);
    }).catch(function(error){
        qTrans.reject(error);
    });

    return qTrans.promise;
};

Thus I try to update the value at the location childRef. When I try it as I do then it does not return any promise. Basically nothing happens.

I also tried the example from the Firebase docs, but that keeps returning null for both post and success.

Upvotes: 2

Views: 1171

Answers (1)

WJA
WJA

Reputation: 7004

Okay figured it out. You can basically set an initial value and decrease it with --.

function fbTransaction(childRef) {
    var qTrans = Q.defer();
    firebase.database().ref(childRef).transaction(function(post) {
        if (post != null) {
            post++;
        } else {
            post = 1;
        }
        return post;
    }).then(function(success){
        qTrans.resolve();
    }).catch(function(error){
        qTrans.reject(error);
    });
    return qTrans.promise;
};

Upvotes: 2

Related Questions