0

i'm so new in firebase, so i need some very basic help.

I have collection "Users" in firestore, whene i have documents with key (user id) and every document have some fields.

enter image description here

In my component i have variable userData: Observable<any[]>

After user login i want find his id in documents and get his info to userData variable.

  ...
  .then(user => {
    this.authState = user;

    // Here i want get user data and set it to my variable
    this.userData = this.afs.collection('Users').document(user.uid);


    this.router.navigate(['/']);
  })

But obviously, this is does not work. I will be gratfull for any help or tips with my question, thanks&

1 Answer 1

1

Rather than doing all of this in the .then after the login Promise; map the AngularFireAuth's authState observable in your init.

this.userData = this.afAuth.authState
  .switchMap { user => user ? this.afs.document("Users/${user.uid}").valueChanges() : of(null) }

So now that we have an Observable of the current user's data over time or null.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.