0

I am transitioning a Firebase real-time database to a Firebase Firestore database but am having trouble finding the appropriate reference to query the current user.

onAuthUserListener = (next, fallback) =>
this.auth.onAuthStateChanged(authUser => {
  if (authUser) {
    this.user(authUser.uid)
      .once('value')
      .then(snapshot => {
        const dbUser = snapshot.val();

        // default empty roles
        if (!dbUser.roles) {
          dbUser.roles = [];
        }

        // merge auth and db user
        authUser = {
          uid: authUser.uid,
          email: authUser.email,
          emailVerified: authUser.emailVerified,
          providerData: authUser.providerData,
          ...dbUser,
        };

        next(authUser);
      });
  } else {
    fallback();
  }
});

Most specifically, what would be the replacement for once('value') and snapshot.val();?

I had thought that

.onSnapshot(snapshot => {
  const dbUser = snapshot.val();
  ...
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Darren
  • 2,176
  • 9
  • 42
  • 98
  • Answer on the equivalent of `once('value'` is below, but I have no idea what ` this.user(authUser.uid)` is. – Frank van Puffelen Dec 11 '18 at 23:06
  • Thank you Frank. ` this.user(authUser.uid)` is just my way of accessing the plugin via `context`. Cheers for your answer, I'll give it a go now. – Darren Dec 11 '18 at 23:15
  • I am struggling with the same problem as you. Did you figure this out? I'm not smart enough to be able to make the leap from the firebase documentation to writing something that works to get a user from firestore that has the same uid as authUser in the Authentication context. Did you find a way to do this? – Mel Jan 09 '20 at 04:52

1 Answers1

3

The equivalent of once('value' in Firestore is called get(), and the equivalent of val() is data(). Calling get() returns a promise, so:

.get().then(snapshot => {
  const dbUser = snapshot.data();
  ...

If you have a collection of users, where the profile of each user is stored within a document with their UID as its ID, you can load that with:

firebase.firestore().collection('users').doc(authUser.uid)
  .get()
  .then(snapshot => {
    const dbUser = snapshot.val();

Note that this is pretty well covered in the documentation on getting data, so I'd recommend spending some time there and potentially taking the Cloud Firestore codelab.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I tried this but there was no .then() method directly in .doc(uid).get(). As said in https://github.com/angular/angularfire/issues/1413#issuecomment-356352970, you have to get a ref object like this: firebase.firestore.collection('users').doc(authUser.uid).ref.get().then( ... ); – GusSL Oct 19 '19 at 17:59
  • 2
    This question was not related to AngularFire, while the link you provide is very specific to that library. If you're having a problem calling the `then` method, I suggest you open a new question and add the [minimal, complete/standalone code that reproduces that problem](http://stackoverflow.com/help/mcve). – Frank van Puffelen Oct 19 '19 at 18:19