1
votes

I am trying to get a simple (in my mind) 2 step query from the firestore using AngularFire2 but I can't seem to understand yet what to do.

    getProfile(): Observable<User>{
    return this.afAuth.authState.map(user => {          
      return this.db.doc<User>('user/'+user.uid).valueChanges();
    });
  }

I keep getting the following: Type 'Observable>' is not assignable to type 'Observable'. Type 'Observable' is not assignable to type 'User'. Property 'name' is missing in type 'Observable'.

What, pray tell, am I missing here

2
did you try with: getProfile(): Observable<any> ? - Harald Wiesinger
I did, but not the solution I am looking for, because then I have to subscribe and subscribe again. - Stephan Grobler

2 Answers

0
votes

So after looking at the FireBase documentation here, my recommendation would be as follows:

getProfile(): Observable<User>{
  return this.afAuth.authState.map(user => {          
    return this.db.doc<User>('user/'+user.uid)
                                      .valueChanges()
                                      .map(fUser=>{
                                            // code here to construct user object if needed
                                           return fUser;
                                       });
  });
}

Explanation

Since FireBase have their variables stored using the following design:

user: Observable<any[]>';
constructor( ... ){
    this.items = db.collection('items').valueChanges();
}

You need to extract the individual user object from the Observable returned from valueChanges(), either by means of using the map() function.

I also created a StackBlitz here to show a proof of concept.

0
votes

So I seem to have been correct that the answer is simple:

   getProfile$(): Observable<User>{
    return this.afAuth.authState.flatMap(user => {
      return this.db.doc<User>('user/'+user.uid).valueChanges();
    });
  }

Not good to try to understand Observables @ midnight