user8516249
user8516249

Reputation:

AngularFire - Firestore - Check if a document exists whilst performing queries

I've seen examples of how to check if a specific document exists. However, is it possible to check if documents exist whilst performing a query like the following?

private albumsCollection: AngularFirestoreCollection<any>;
albums: Observable<any[]>;

...


this.albumCollection = this.afs.collection<any>(`albums`, ref => {
  return ref.where('albumid', "==", this.albumid);
});

Upvotes: 2

Views: 5702

Answers (1)

Troy Myers
Troy Myers

Reputation: 520

If I understood correctly, you could do something along the lines of this:

this.afs.collection(`albums`, ref => ref.where('albumid', "==", this.albumid)).snapshotChanges().subscribe(res => {
    if (res.length > 0)
    {
    console.log("Match found.");
    }
    else
    {
    console.log("Does not exist.");
    }
});

If the array of snapshots isn't empty then you have albums that match the ID you are querying for. Then by using snapshotChanges you could get the key of each document and their values.

Upvotes: 9

Related Questions