Search code examples
angularfirebasegoogle-cloud-firestoreangularfire

Angularfire error: "Property 'subscribe' does not exist on type 'Promise<any>'."


When I fetch data from database, it shows this error:

"Property subscribe does not exist on type Promise<any>."

How can I solve this error?

This is my code:

getUsers(){
    return new Promise<any>((resolve, reject) => {
      this.afs.collection('/users').snapshotChanges()
      .subscribe(snapshots => {
        resolve(snapshots)
      })
    })
  }

Solution

  • I think that elsewhere in your code, you are trying to subscribe to the promise that getUsers() returns. Unlike Observables, Promises cannot be subscribed to. More on that here: What is the difference between Promises and Observables?

    I don't know the overall goal of your Angular code, but I believe you should be able to get away with:

    getUsers(){
        return this.afs.collection('/users'.snapshotChanges()
    }
    

    As this returns an Observable, which you can then subscribe to (or use async to synchronize your displayed data to your database).