FiringBlanks
FiringBlanks

Reputation: 2084

Firestore transaction - Type error

I'm following along with the documentation (first example here) but I'm getting an error for some reason. It's expecting an argument of type DocumentReference but the variable I'm passing in is of type AngularFirestoreDocument<{}>. I can't seem to cast it. Here's the code:

      //Get Pins counter
      let pathRef = 'PlaceOne/'+this.Place2;
      var pinDocRef = this.afs.doc(pathRef);

      //Run Transaction
      return this.afs.firestore.runTransaction(function(transaction){
        return transaction.get(pinDocRef).then(function(pinDoc){
          if(!pinDoc.exists){
            throw "Document does not exist!"
          }

          var newPinScore = pinDoc.data().pins + 1;
          transaction.update(pinDocRef, { pins: newPinScore });
        });
      })

Gives me this error:

Transaction error

Upvotes: 0

Views: 1087

Answers (1)

Hareesh
Hareesh

Reputation: 6900

You can achieve it without angularfire using firebase native method.

import * as firebase from 'firebase';

Then inside your function

let pinDocRef = firebase.firestore().collection('PlaceOne').doc(this.Place2);

  return firebase.firestore().runTransaction(function(transaction) {
    // This code may get re-run multiple times if there are conflicts.
    return transaction.get(pinDocRef).then(function(pinDoc) {
        if(!pinDoc.exists){
            throw "Document does not exist!"
        }

        let newPinScore = pinDoc.data().pins + 1;
        transaction.update(pinDocRef, { pins: newPinScore });
    });
  }).then(function() {
      console.log("Transaction successfully committed!");
  }).catch(function(err) {
      console.log("Transaction failed: ", err);
  });

If you want to use Angularfire way try

var pinDocRef = this.afs.doc(pathRef).ref;

i am not sure about the second way.

Upvotes: 2

Related Questions