1

I'm using rxjs map to retrive data in firestore like this:

  getArtists(): Observable<DocumentData> {
    const users$ = this.firestore.collection('/Users').get()
    users$.subscribe((users) => {
      users.docs.map(user => user.data().artistName !== "" && user.data().role === 'ARTIST')
    });
    return users$;
  }

but when i'm getting value like this :

this.userService.getArtists().subscribe(
      (userDocs) => {
        userDocs.docs.map((user) => {
          this.artists.push(user.data());
          console.log(this.artists)
          this.record = this.artists.length;
        })
      });

it's return always the user when the artistName is equals to "" and role is not equals to 'ARTIST'. why ?

thank's everybody!

satanTime
  • 12,631
  • 1
  • 25
  • 73
Alexis Lapeze
  • 69
  • 2
  • 10

1 Answers1

2

you need to map data in a map operator instead of a subscription and return a value in as a pipe. Unfortunately, in your code isn't clear what and when you want to filter, why a user is in users.docs when it tend to be a doc.

Please check an example below and consider updating your question with more info.

import {filter, map} from 'rxjs/opreators';

  getArtists(): Observable<DocumentData> {
    return this.firestore.collection('/Users').get().pipe( // <- use pipe
       map(users => {
          // here some changes in users if we need.
          return users;
       }),
    ),
    filter(users => {
      returns true; // or false if we don't want to emit this value.
    }),
  }
satanTime
  • 12,631
  • 1
  • 25
  • 73
  • i resolve like this : getArtists(): Observable { return this.firestore.collection('/Users').get().pipe( map((users) => users.docs.filter(user => user.data().artistName !== "" && user.data().role === 'ARTIST').map(user => user) )) } – Alexis Lapeze May 05 '20 at 07:19
  • but how can i get value of pipe in my component ? – Alexis Lapeze May 05 '20 at 07:19
  • the same as you do in your code via `subscribe` = `this.userService.getArtists().subscribe`. The goal is to subscribe only when you need data to display somewhere. If you need to filter / transform / join etc - use pipes. The solution in your comment is fine, but `.map(user => user)` is redundant, it doesn't change anything. – satanTime May 05 '20 at 07:28