如何使用多个.Where()搜索? Vue JS和Firebase

时间:2020-09-06 05:43:33

标签: firebase vue.js google-cloud-firestore conditional-statements where-clause

我想查询多个馆藏的搜索,而不仅仅是品牌。我直接从Cloud Firestore查询

到目前为止,我已经成功查询了品牌。

我也尝试将类别和描述结合在一起,但是总是失败

我使用where()来直接从Firebase调用

getExchanges({commit, state}, {searched} = {searched: ''}) {
      commit('resetPagination');
      // Here you want to make a call to firebase and ask for data
      return db
       .collection('exchanges')
       .where('brand', '>=', searched).where('brand', '<=', searched+ '\uf8ff')
       .limit(state.pagination.itemCount)
       .get()
       .then(snapshots => {
         if (snapshots.docs.length === 0) {
            commit('setExchanges', []);
            return []
         }
         const exchanges = snapshots.docs.map(doc => ({...doc.data(), id: doc.id}))
         commit('setExchanges', exchanges)
         commit('setLastItem', snapshots.docs[snapshots.docs.length - 1])
         commit('setPreviousFirstItem', snapshots.docs[0])
         return exchanges
        })

我在Firestore的交换集中的字段是

  • 品牌
  • 类别
  • 城市

对于类别,我尝试这样做

.where('brand' || 'category', '>=', searched).where('brand' || 'category', '<=', searched+ '\uf8ff')

您有什么建议吗?太神奇了

欢呼

Zaid

1 个答案:

答案 0 :(得分:1)

当前,Cloud Firestore不支持“或”查询。您可以使用的最接近的方法是使用“ array-contains”和“ array-contains-any”查询,并明确设计数据结构以适合查询需求。一种替代方法是使用名为Algolia的第三方服务。

要注意的另一点是您的代码

.where('brand' || 'category', '>=', searched).where('brand' || 'category', '<=', searched+ '\uf8ff')

相同
.where('brand', '>=', searched).where('brand', '<=', searched+ '\uf8ff')

如果第一项未定义,为null,空字符串,空数组或false,则JavaScript中的||运算符将返回第二项,否则返回第一项。