skip to Main Content

When I try to subscribe to collectionData or docData, I always get this error "ERROR FirebaseError: Expected type ‘Query’, but it was: a custom dh object". The weird part is I have an app using Angular 15 and I do NOT have this issue there. This app is Angular 16, and I have it setup the same exact way. I’m not sure if the Angular version has anything to do with it.

import { Firestore, collection, collectionData, query } from '@angular/fire/firestore';

constructor(
    private firestore: Firestore
  ) { }

getNotes(user: string) : Observable<Notes[]> {
    const collectionRef = collection(this.firestore, `notes/${user}/notes`)
    const q = query(collectionRef)
    var notes$ = collectionData(q, {idField: 'id'}) as Observable<Notes[]>
    notes$.subscribe(notes => {
      console.log(notes)
    })
  }

If I use getDocs, with "then" instead of "subscribe", that seems to work. But that is just a one time call, and I want to listen for changes. I have looked all over the internet for an answer, but anything like this is usually because people are using two different libraries. I am definitely not. I hope someone can help!

2

Answers


  1. Here is an updated version of your getNotes() function that fixes the error:

    import { Firestore, CollectionReference, Query } from '@angular/fire/firestore';
    
    constructor(private firestore: Firestore) {}
    
    getNotes(user: string): Observable<Notes[]> {
      const collectionRef: CollectionReference = this.firestore.collection(`notes/${user}/notes`);
      const q: Query<Notes> = query(collectionRef);
      const notes$: Observable<Notes[]> = this.firestore.collectionData(q, { idField: 'id' });
    
      notes$.subscribe(notes => {
        console.log(notes);
      });
    
      return notes$;
    }
    
    Login or Signup to reply.
  2. This may be down to the Angular Fire and Firebase version you are using. There is incompatibility between Angular Fire 7 and Firebase 10. Check which versions you are using in package.json. If you have firebase 10+ then downgrade to"firebase": "^9.23.0". Also add "rxfire": "^6.0.3" if needed. Then retry.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search