您可以创建动态Firebase查询吗? [英] Can you create dynamic firebase queries?

查看:34
本文介绍了您可以创建动态Firebase查询吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想像这样查询firebase,但不允许我这样做.有没有办法动态查询Firebase?我需要处理非常具体的情况-例如,如果用户选择不重要",那么我需要从查询中删除该首选项过滤器,某些字段是数组数据类型,某些字段是字符串,有些字段是数字.我需要能够处理所有形式的数据类型,并适当地填充where子句.手动检查每个字段并调出每个字段将对系统造成负担(有20多个字段可供查询).任何建议都会很棒!

I want to query firebase like so but it doesn't allow me. Is there a way to dynamically query firebase? I need to handle very specific cases - for example if the user selects "Doesn't Matter" then I need to remove that preference filter from the query, some of the fields are array datatypes and some are strings and some are numbers. I need to be able to handle all forms of datatypes and populate the where clause appropriately. Manually checking each field and calling out for each field will be get taxing on the system (there are over 20 fields to query on). Any advise would be great!

for(ddp in docDataPreferences){
  if(docDataPreferences[ddp] == "Doesn't Matter"){
    allDoesntMatterPreferences.push(ddp);
  } else if(docDataPreferences[ddp] != "Doesn't Matter" && docDataPreferences[ddp] != '' && docDataPreferences[ddp] != undefined) {
    keysWithValues.push(ddp);
    whereClause += ".where('preferences."+ ddp + "', 'array-contains-any', " + JSON.stringify(docDataPreferences[ddp]) +")"
  }
}

var usersMatchesCollection = config.db.collection("Users");
var query = usersMatchesCollection + whereClause;
await query.get().then( function (matchesQuerySnapshot) {
  matchesQuerySnapshot.forEach(function(doc) {
      //...do something
  })
})

TypeError:query.get不是一个函数.(在"query.get()"中,"query.get"未定义)]

TypeError: query.get is not a function. (In 'query.get()', 'query.get' is undefined)]

我可以看到where子句可以正确打印出来,但是我认为您无法连接对象和字符串.当我手动添加返回的whereClause时,如下所示:

I can see that the where clause is printing out correctly but I assume you can't concat the object and the string. When I manually add the returned whereClause like so:

var query = usersMatchesCollection.where('preferences.datingPrefDistance', '==', 22).where('preferences.datingPrefDrinking', '==', "No").where('preferences.datingPrefEducation', '==', "Doctorate").where('preferences.datingPrefKids', '==', "No").where('preferences.datingPrefMarried', '==', "No").where('preferences.datingPrefSmokingCig', '==', "No").where('preferences.datingPrefSmokingPot', '==', "No").where('preferences.prefDrinking', '==', "No").where('preferences.prefEducation', '==', "Doctorate").where('preferences.prefIdentifyAs', '==', "Male").where('preferences.prefKids', '==', "No").where('preferences.prefMarried', '==', "No").where('preferences.prefSmokingPot', '==', "No")

有效.因此,导致问题的是串联.

it works. So it is the concatenation that is causing an issue.

周围有工作吗?

推荐答案

似乎您正在尝试通过连接 CollectionReference 和一个字符串来构造查询,但这无法正常工作.如果您使用 console.log(query),则会看到它是一个字符串,而不是 Query ,并且因为没有 get()字符串上的方法,它解释了您得到的错误.

It seems that you're trying to construct a query by concatenating a CollectionReference and a string, which won't work. If you console.log(query), you'll see that it's a string, instead of a Query, and since there's no get() method on a string, that explains the error you get.

要构建具有动态条件的查询,应遵循以下模式:

To build a query with dynamic condition, you should follow this pattern:

var usersMatchesCollection = config.db.collection("Users");
var query = usersMatchesCollection;

for(ddp in docDataPreferences){
  if(docDataPreferences[ddp] == "Doesn't Matter"){
    allDoesntMatterPreferences.push(ddp);
  } else if(docDataPreferences[ddp] != "Doesn't Matter" && docDataPreferences[ddp] != '' && docDataPreferences[ddp] != undefined) {
    keysWithValues.push(ddp);
    query = query.where('preferences.'+ ddp, 'array-contains-any', docDataPreferences[ddp]))
  }
}

await query.get().then( function (matchesQuerySnapshot) {
  matchesQuerySnapshot.forEach(function(doc) {
      //...do something
  })
})

问题的关键在于 query = query.where 不断变化,用每个条件的另一个查询替换查询,添加新条件.然后,在处理完所有条件后,您可以执行最终查询.

The crux here is that query = query.where keeps changing replacing the query with another query for each condition, adding the new condition. Then, once all conditions are processed, you can execute the final query.

我试图确保代码与您的代码匹配,但是由于我不得不猜测 docDataPreferences 包含的内容,因此该行中可能存在一两个语法错误.

I tried to ensure the code matches yours, but there might be a syntax error or two in that line as I had to guess what docDataPreferences contains.

这篇关于您可以创建动态Firebase查询吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆