Search code examples
dictionaryrxjsrxjs-observables

why map with condition return always value


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!


Solution

  • 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.
        }),
      }