AngularFire - Firestore - 在执行查询时检查文档是否存在

2 firebase angularfire2 angular google-cloud-firestore

我看过有关如何检查特定文档是否存在的示例。但是,是否可以在执行如下查询时检查文档是否存在?

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

...


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

小智 9

如果我理解正确,您可以按照以下方式做一些事情:

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.");
    }
});
Run Code Online (Sandbox Code Playgroud)

如果快照数组不为空,那么您的相册与您查询的 ID 相匹配。然后通过使用 snapshotChanges 可以获得每个文档的键和它们的值。