1
votes

I am trying to obtain a single element (path) for a specific user within Firestore using angularfire2 / angular 6.

I currently store the image into the Firestore database. I then add a record into the Firestore database with the users UID and path of the file.

I am attempting to get a single item (path) by looking up the users UID. I know how to use queries in Firestore. I want to get the path WITHOUT a subscription.

Anyone know how to pull data from Firestore without a subscription?

getUserImage(uid) {
      const imageRef = this.afs.collection('/users', ref => ref.where('userUID', '==', uid));
      return imageRef.snapshotChanges().pipe(map(results1 => {
        return results1.map((x) => {
          return x.payload.doc.data() as User;
        });
       }));
    }

Example: If I am pulling information about a single user; Can i do this without a subscription?

2
not sure you understand what you're askingStavm
Is there a way to get data from FireStore without using .subscribe?Kevin Summersill

2 Answers

3
votes

You can use the plain old JavaScript API for getting a document. It exposes a get() method that performs a one-time retrieval of a document. From the linked documentation:

var docRef = db.collection("cities").doc("SF");

docRef.get().then(function(doc) {
    if (doc.exists) {
        console.log("Document data:", doc.data());
    } else {
        // doc.data() will be undefined in this case
        console.log("No such document!");
    }
}).catch(function(error) {
    console.log("Error getting document:", error);
});

All you need is a Firestore db instance to get started.

0
votes

The only way to get data from a FireStore collection is through a subscription; however, you can manipulate the stream of data prior to subscription by using RXJS commands. An example of this is:

In this example, this.afs is referencing my AngularFireStore then I go about collecting the information of a specific user. I then run a snapshotChanges() function provided by FirebaseStore.

Now in order to get the specific data that you are looking for you to want to start all RXJS 6 with a .pipe. Think of this as the Mario in a tunnel. You start with the pipe first. Now you want to use the MAP operator to change what it is that you want. In this instance, I want to map then get the payload.

Now you could run .data() function to return all your data; however, I run the .get data because I only want to return specific data.

Once you have decided what it is that you want you can then subscribe which will return what you specify in the pipe.

  loadUser(user) {
    this.afs.doc<any>(`users/${user}`).snapshotChanges().pipe(
      map(actions => {
        console.log('Getting image reference from user');
        const imageRef = actions.payload.get('appPhotoRef');
        console.log(imageRef);
        return imageRef;
      })
      ).subscribe((data) => {
      console.log('Getting download URL');
      console.log(data);
      return this.getDownloadURL(data);
    });
  }