我可以在AngularFire的查询中创建一个连接字符串吗?

我可以在AngularFire的查询中创建一个连接字符串吗?,angular,typescript,google-cloud-firestore,angularfire,querying,Angular,Typescript,Google Cloud Firestore,Angularfire,Querying,我试图在一个有Angular 8的网站上制作一个动态搜索表单,用户可以使用不同的下拉菜单来选择在Firestore中搜索什么。根据不同的选择,我有一个函数,可以生成一个与查询应该具有相同形式的字符串,尽管它是一个字符串。但我不知道如何将它与valueChanges()一起使用,因为它仍然是一个字符串。这可能吗 我想这不是一种非常优雅(如果可能的话)的动态查询方式,但如果可能的话,我认为这会为我节省宝贵的时间。(我还了解了如何使用行为主题和开关映射制作过滤器,因此如果这不起作用,我想这是另一种(更

我试图在一个有Angular 8的网站上制作一个动态搜索表单,用户可以使用不同的下拉菜单来选择在Firestore中搜索什么。根据不同的选择,我有一个函数,可以生成一个与查询应该具有相同形式的字符串,尽管它是一个字符串。但我不知道如何将它与
valueChanges()
一起使用,因为它仍然是一个字符串。这可能吗

我想这不是一种非常优雅(如果可能的话)的动态查询方式,但如果可能的话,我认为这会为我节省宝贵的时间。(我还了解了如何使用行为主题和
开关映射制作过滤器,因此如果这不起作用,我想这是另一种(更好的?)方法。)

异步getRealTimeData(值){
this.query=等待this.makeQuery(值);
this.data=this.query.valueChanges();
}
异步makeQuery(值){
var集合:字符串;
开关(value.collection){
案例“X”:
集合='X';
this.queryString=“.where('datetime','>=','2020-01-15T09:51:00.000Z')”;
打破
案例“Y”:
集合='Y';
this.queryString=“.orderBy('ID','asc')”;
打破
}
//如果选择Z,则添加到搜索字符串
if(值.Z){
this.queryString=this.queryString.concat(“.where('Z','=',value.Z)”);
}
//如果不是viewAllUser,请添加允许的
else if(this.authService.viewaluser==false){
this.queryString=this.queryString.concat(“.where('ID','in',this.allowed”);
}
this.queryString=this.queryString.concat(.orderBy('datetime','desc'));
//如果是实时的,则为搜索字符串添加限制
//(如果下载:无限制)
if(this.searchType==“realtime”){
this.queryString=this.queryString.concat('.limit(100)');
}
this.query=this.query.concat(this.queryString.concat(');
log('Query:',this.Query);
返回此.query;
}

您将希望停止使用字符串定义查询。要将字符串转换为可执行代码,您必须
eval()
is,这在许多环境中都不是安全的操作。但也不需要它,因为您也可以使用类似的模式来构建查询

async makeQuery(value) {
  switch (value.collection) {
    case 'X':
      this.query = this.query.where('datetime', '>=', '2020-01-15T09:51:00.000Z');
      break;
    case 'Y':
      this.query = this.query.orderBy('ID', 'asc');
      break;
  }

  // If Z chosen, add to query
  if (value.Z) {
    this.query = this.query.where('Z', '==', value.Z);
  }
  // If not viewAllUser, add list of permitted
  else if (this.authService.viewAllUser == false) {
    this.query = this.query.where('ID', 'in', this.permitted);
  }
  this.query = this.query.orderBy('datetime', 'desc');
  // If realtime, add limit to search string
  // (If download: no limit)
  if (this.searchType == "realtime") {
    this.query = this.query.limit(100);
  }

  return this.query;
}

您将看到代码仍然与您的代码非常相似,但现在构建的是实际的查询,而不是串接字符串。

谢谢!是的,我也从你之前的一个答案()中意识到这是一种非常不必要的做事方式!