0

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)
      })
    })
  }
Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
  • Look at [this](https://github.com/angular/angularfire2/blob/master/docs/firestore/collections.md). You are performing a method which does not immediately finish running, so you have to wait for the result – tomerpacific Aug 21 '19 at 09:27

1 Answers1

1

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).

jules0075
  • 28
  • 6