0

I have below code in angular service using firestore, there is not syntax error and it's working fine,

The problem is the returning type, since I am using take(1) I want the return type be Observable<Client>

fetchByUserId(uid: string): Observable<Array<Client>> {
    return this.collection(ref => ref.where(`users.${uid}`, '>', 0))
      .valueChanges()
      .take(1);
  }

I tried using .first() but problem is by using first observable completes and I want to keep streaming.

Reza
  • 18,865
  • 13
  • 88
  • 163
  • Take(1) and first() take an emitted 'value' from observables rather than fetching the first element in an emitted 'value'. You need to use map to transform the value of an array to an object. – yeh Apr 15 '18 at 22:59
  • `.take(1).map(([element]) => element)` – cartant Apr 15 '18 at 23:00

1 Answers1

1

.first() and .take(1) will actually do (almost) the same thing in your case.

What you want is map the result array to the first element of your array.

You should be able to insert a map like this :

return this.collection(...)
    .map(arr => arr[0])

this is assuming this.collection(...) actually returns an Observable<Array<Client>>, and in this case the result of this map will return an Observable<Client>

Pac0
  • 21,465
  • 8
  • 65
  • 74