AngularFirestoreDocument doesn't satisfy the DocumentRef type expected by firestore

1,346 views
Skip to first unread message

Doug Snyder

unread,
Jun 29, 2019, 2:11:12 AM6/29/19
to Firebase Google Group
I noticed there are no docs for angular-firestore2, only a set of tutorials
Batch writes are covered so I'm partly using the Javascript docs from Firebase

export class FinancialService {

  private transCollection: AngularFirestoreCollection<Transaction>;

  batch_update_trans(trans) {
    let writer = this.afs.firestore.batch();
    this.transCollection = this.afs.collection<Transaction>('balance_sheets/'+this.balance_sheet_id+"/transactions/");
    for (let t of trans) {
        writer.update(this.transCollection.doc(t.id),t);
    }
    return writer.commit(); 
   }
}

This doesn't compile with this error:
error TS2345: Argument of type 'AngularFirestoreDocument<{}>' is not assignable to parameter of type 'DocumentReference'.
      Type 'AngularFirestoreDocument<{}>' is missing the following properties from type 'DocumentReference': id, firestore, parent, path, and 2 more.

If I cast the AngularFirestoreDocument to <any> 
writer.update(<any> this.transCollection.doc(t.id),t);
it compiles but is causes a runtime error:
ERROR Error: Uncaught (in promise): FirebaseError: [code=invalid-argument]: Function WriteBatch.update() requires its first argument to be a DocumentReference, but it was: an object

If I try to cast the AngularFirestoreDocument to DocumentReference:
writer.update(<DocumentReference> this.transCollection.doc(t.id),t);
It does compile because the types don't overlap each other:
Conversion of type 'AngularFirestoreDocument<{}>' to type 'DocumentReference' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
      Type 'AngularFirestoreDocument<{}>' is missing the following properties from type 'DocumentReference': id, firestore, parent, path, and 2 more.

If I try to cast AngularFirestoreDocument to <unknown>
writer.update(<unknown> this.transCollection.doc(t.id),t);
it fails to compile:
error TS2345: Argument of type 'unknown' is not assignable to parameter of type 'DocumentReference'.

It appears its impossible to do a batch write in angular-firestore,
but then again since there's no documentation, 
maybe I'm just doing it wrong?

Kato Richardson

unread,
Jun 30, 2019, 2:13:01 PM6/30/19
to Firebase Google Group
Hi Doug,

These aren't meant to be interchangeable. You'll be better off just using the Firebase SDK directly here.

for (let t of trans) {
   const doc = this.afs.firestore.doc(`balance_sheets/${this.balance_sheet_id}/transactions/${t.id}`);
   writer.update(doc, t);
}

If you happen to have an AngularFirestoreCollection and need from there to a DocumentReference for some reason, you can do so using snapshotChanges():

Why would you use it? - When you need the document data but also want to keep around metadata. This metadata provides you the underyling DocumentReference and document id. 

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/b0b86ec6-5da0-455b-8ae5-7a17670b5749%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Reply all
Reply to author
Forward
0 new messages