Firebase 如何使firestore查询与get()和valueChanges()一起使用?

Firebase 如何使firestore查询与get()和valueChanges()一起使用?,firebase,google-cloud-firestore,angularfire2,querying,Firebase,Google Cloud Firestore,Angularfire2,Querying,我使用Angular 8,有一个表单,用户可以选择要查询数据库的内容,然后单击两个按钮中的任意一个-一个按钮在网站上实时查看数据,另一个按钮下载数据 我想我可以使用一个函数进行查询,然后根据用户单击的按钮调用不同的函数,使用get()进行下载,使用valueChanges()进行实时数据视图。但当我尝试此操作时,在浏览器控制台中会出现以下错误。(这是使用query作为类型any-如果我将类型指定为AngularFirestoreCollection,我会在VSCode中获得有关get()部分的类

我使用Angular 8,有一个表单,用户可以选择要查询数据库的内容,然后单击两个按钮中的任意一个-一个按钮在网站上实时查看数据,另一个按钮下载数据

我想我可以使用一个函数进行查询,然后根据用户单击的按钮调用不同的函数,使用
get()
进行下载,使用
valueChanges()
进行实时数据视图。但当我尝试此操作时,在浏览器控制台中会出现以下错误。(这是使用
query
作为类型
any
-如果我将类型指定为AngularFirestoreCollection,我会在VSCode中获得有关
get()
部分的类型的错误)

错误:“未捕获(承诺中):TypeError:this.query.get不是 函数

我可以补充一点,我以前有两个完全独立的(工作)功能用于实时下载和查看。下载时我使用了下面的查询。我推测这实际上是一个Firestore查询,而“我试图在更新的代码中使用AngularFirestoreCollection。但是有没有一种方法可以使某种查询/收集同时适用于
get()
valueChanges()

旧(工作)查询:

var query = this.afs.collection(collection).ref.where('module', 'in', array_part);
  onSubmit(value, buttonType): void {
    if (buttonType=='realtime') {
      this.getRealTimeData(value);
    }
    if (buttonType=='download') {
      this.downloadCsv(value);
    }
  }

  async downloadCsv(value) {
    this.query = this.makeQuery(value);
    this.dataForDownload = await this.getDataForDownload();
    this.dataForDownload = JSON.stringify(this.dataForDownload['data']);
    console.log('Data: ', this.dataForDownload);
    var date = new Date();
    var date_str = this.datePipe.transform(date, 'yyyy-MM-ddTHH-mm');
    this.makeFileService.downloadFile(this.dataForDownload, 'OPdata-' + date_str);
  }

  getDataForDownload() {
    return this.query.get()
      .then(function (querySnapshot) {
        var jsonStr = '{"data":[]}';
        var dataObj = JSON.parse(jsonStr); //making object we can push to
        querySnapshot.forEach(function (doc) {
          JSON.stringify(doc.data()), ', id: ', doc.id);
          dataObj['data'].push(doc.data());
        });
        return dataObj;
      })
      .catch(function (error) {
        console.log("Error getting documents: ", error);
      });
  }

  async getRealTimeData(value) {
    this.query = await this.makeQuery(value);
    this.data = this.query.valueChanges();
  }

  async makeQuery(value) {
    var collection: string;
    return this.query = this.afs.collection<DataItem>('CollectionName', ref => ref.where('datetime', '>=', '2020-01-15T09:51:00.000Z').orderBy('datetime', 'desc').limit(100));
  }
尝试一个公共函数makeQuery():

var query = this.afs.collection(collection).ref.where('module', 'in', array_part);
  onSubmit(value, buttonType): void {
    if (buttonType=='realtime') {
      this.getRealTimeData(value);
    }
    if (buttonType=='download') {
      this.downloadCsv(value);
    }
  }

  async downloadCsv(value) {
    this.query = this.makeQuery(value);
    this.dataForDownload = await this.getDataForDownload();
    this.dataForDownload = JSON.stringify(this.dataForDownload['data']);
    console.log('Data: ', this.dataForDownload);
    var date = new Date();
    var date_str = this.datePipe.transform(date, 'yyyy-MM-ddTHH-mm');
    this.makeFileService.downloadFile(this.dataForDownload, 'OPdata-' + date_str);
  }

  getDataForDownload() {
    return this.query.get()
      .then(function (querySnapshot) {
        var jsonStr = '{"data":[]}';
        var dataObj = JSON.parse(jsonStr); //making object we can push to
        querySnapshot.forEach(function (doc) {
          JSON.stringify(doc.data()), ', id: ', doc.id);
          dataObj['data'].push(doc.data());
        });
        return dataObj;
      })
      .catch(function (error) {
        console.log("Error getting documents: ", error);
      });
  }

  async getRealTimeData(value) {
    this.query = await this.makeQuery(value);
    this.data = this.query.valueChanges();
  }

  async makeQuery(value) {
    var collection: string;
    return this.query = this.afs.collection<DataItem>('CollectionName', ref => ref.where('datetime', '>=', '2020-01-15T09:51:00.000Z').orderBy('datetime', 'desc').limit(100));
  }
onSubmit(值,按钮类型):void{
如果(按钮类型=='realtime'){
此.getRealTimeData(值);
}
如果(按钮类型=='download'){
下载CSV(值);
}
}
异步下载CSV(值){
this.query=this.makeQuery(值);
this.dataForDownload=等待这个.getDataForDownload();
this.dataForDownload=JSON.stringify(this.dataForDownload['data']);
console.log('Data:',this.dataForDownload);
变量日期=新日期();
var date_str=this.datePipe.transform(日期'yyyyy-MM-ddTHH-MM');
this.makeFileService.downloadFile(this.dataForDownload,'OPdata-'+date_str);
}
getDataForDownload(){
返回此.query.get()
.then(函数(querySnapshot){
var jsonStr='{“数据”:[]}';
var dataObj=JSON.parse(jsonStr);//生成我们可以推送到的对象
querySnapshot.forEach(函数(doc){
stringify(doc.data()),',id:',doc.id);
dataObj['data'].push(doc.data());
});
返回数据对象;
})
.catch(函数(错误){
log(“获取文档时出错:”,错误);
});
}
异步getRealTimeData(值){
this.query=等待this.makeQuery(值);
this.data=this.query.valueChanges();
}
异步makeQuery(值){
var集合:字符串;
返回this.query=this.afs.collection('CollectionName',ref=>ref.where('datetime','>=','2020-01-15T09:51:00.000Z')。orderBy('datetime','desc')。limit(100));
}
是angularfire中用于从firestore检索数据的方法,而
get()
方法用于从firestore检索数据,但使用普通javascript

混合使用这两种方法将返回一个错误,正如您在代码中看到的那样。因此,由于angularfire是在javascript firebase代码之上创建的,因此您应该能够使用
valueChanges()
在网站上实时查看数据,并下载数据。

angularfire中使用的方法是从firestore检索数据,而
get()
方法用于从firestore检索,但使用普通javascript


混合使用这两种方法将返回一个错误,正如您在代码中看到的那样。因此,由于angularfire是在javascript firebase代码之上创建的,因此您应该能够使用
valueChanges()
在网站上实时查看数据,并下载数据。

谢谢,我将尝试一下!我认为
get
方法用于一次性获取数据的用例,而
valueChanges
用于实时查看。但是我想用管道和take(1)使用
valueChanges
也许同样好?你能给我举个例子,说明如何使用valueChanges()下载数据吗?当我使用
get()
时,我制作了一个JSON格式的字符串,可以下载,但我没有让它与valueChanges()一起使用。不要获取任何历史记录,只获取已更改的内容…@Ingrid请用
get()
valueChanges()
提出不同的问题。还有其他一些方法,如
snapshotChanges()
,可以为您提供元数据。我想这就是您想要的。这基本上是同一个问题,但可能更清楚我想要什么:谢谢,我会试试这个!我认为
get
方法用于一次性获取数据的用例,而
valueChanges
用于实时查看。但是我想用管道和take(1)使用
valueChanges
也许同样好?你能给我举个例子,说明如何使用valueChanges()下载数据吗?当我使用
get()
时,我制作了一个JSON格式的字符串,可以下载,但我没有让它与valueChanges()一起使用。不要获取任何历史记录,只获取已更改的内容…@Ingrid请用
get()
valueChanges()
提出不同的问题。还有其他一些方法,如
snapshotChanges()
,可以为您提供元数据。我想这就是您想要的。这基本上是同一个问题,但可能更清楚我想要什么: