Javascript 创建查询快照以侦听实时更改

Javascript 创建查询快照以侦听实时更改,javascript,firebase,google-cloud-firestore,Javascript,Firebase,Google Cloud Firestore,我创建了一个函数,它使用if条件从Firestore获取数据。我已经用.get()方法实现了它,但是它没有得到实时更新,我的意思是,如果服务器中有任何记录被更新,它不会反映在这里 这是我的功能 async getMyPosts() { this.query = firebase.firestore().collection('complaints') if (this.complaint_status_filter_active==true) { // you decid

我创建了一个函数,它使用if条件从Firestore获取数据。我已经用
.get()
方法实现了它,但是它没有得到实时更新,我的意思是,如果服务器中有任何记录被更新,它不会反映在这里

这是我的功能

   async getMyPosts() {
    this.query = firebase.firestore().collection('complaints')
    if (this.complaint_status_filter_active==true) {  // you decide
      this.query = this.query
      .where('complaint_status', '==', this.complaint_status_filter_value)
      .limit(5)
    }
    const questions = await this.query.get()
      .then(snapshot => {
        snapshot.forEach(doc => {
          console.log(doc.data())
        })
      })
      .catch(catchError)
  }
应该使用什么来代替
get()
进行实时更新

编辑1 在下面的示例中,我可以使用Angular Firestore使用快照更改,但在这里,我不能使用
if
条件

如何在此处添加带有if子句的where条件?代码:

  this.firestore.collection('complaints' , ref => ref
.where('complaint_status', '==', this.complaint_status_filter_value)
.limit(5)
.orderBy("complaint_date","desc") 
)
.snapshotChanges()
.subscribe(response => {
用于获取实时更新,如中所示


查看文档,在示例文档中,他们在文档中使用了此选项,您能帮助我在何处使用此选项吗?我已更新了我的问题,在编辑1中,我可以使用快照更改,但无法使用。如果有,请帮助(如果可能)在没有文档的情况下如何使用此选项<代码>db.collection(“cities”).doc(“SF”).onSnapshot(函数(doc){console.log(“当前数据:”,doc.data();}),如果我删除了文档,它会给出错误。如果您试图查询集合并获取实时结果,那么您应该按照文档进行操作。
this.query.onSnapshot(doc => {
    // ...
})