如何根据条件添加火中查询中的状况?

发布于 2025-02-09 03:09:13 字数 1018 浏览 0 评论 0原文

我正在Firebase中编写查询,但是我想根据条件添加where()条件。就像存在某些值一样,应该包括(),否则应按原样运行普通查询。以下是我的代码。我使用console.log检查了条件,但查询返回所有结果。请帮助我解决此问题。

const loadPapers = () => {
  let t = savedTopifyPapers
  if (queryParams.value.key && queryParams.value.value) {
    t.orderBy('timeStamp')
      .limit(limit.value)
      .where(queryParams.value.key.toString(), '==', queryParams.value.value)
  } else {
    t.orderBy('timeStamp').limit(limit.value)
  }
  t.onSnapshot((snapshot) => {
    const _lastDoc = snapshot.docs[snapshot.docs.length - 1]
    firstVisible.value = snapshot.docs[0]
    if (_lastDoc) {
      lastDoc.value = _lastDoc
    }
    if (snapshot.empty) {
      loadingData.value = false
    }
    papersData.value = snapshot.docs.map((doc) => {
      const data = doc.data()
      const id = doc.id
      loadingData.value = false
      return { id, ...data }
    })
    papersDataLength.value = papersData.value.length
  })
}

任何解决方案都赞赏!

I am writing a query in firebase, but I want to add where() condition based on the condition. Like if some values are exist then where() should be included otherwise normal query should be run as it is. Below is my code. I checked using console.log if condition is passed but the query returns all results. Please help me to fix this issue.

const loadPapers = () => {
  let t = savedTopifyPapers
  if (queryParams.value.key && queryParams.value.value) {
    t.orderBy('timeStamp')
      .limit(limit.value)
      .where(queryParams.value.key.toString(), '==', queryParams.value.value)
  } else {
    t.orderBy('timeStamp').limit(limit.value)
  }
  t.onSnapshot((snapshot) => {
    const _lastDoc = snapshot.docs[snapshot.docs.length - 1]
    firstVisible.value = snapshot.docs[0]
    if (_lastDoc) {
      lastDoc.value = _lastDoc
    }
    if (snapshot.empty) {
      loadingData.value = false
    }
    papersData.value = snapshot.docs.map((doc) => {
      const data = doc.data()
      const id = doc.id
      loadingData.value = false
      return { id, ...data }
    })
    papersDataLength.value = papersData.value.length
  })
}

Any solution appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光礼记 2025-02-16 03:09:14

只需引用集合
让Query = firebase.firestore()。collection(collectionName)

query = query.where(...)
query = query.where(...)  

并使用条件

if (condition) {
  query = query.where(...)
}  

执行查询后,所有子句添加。

query.get().then(...)

just take reference of collection.
let query = firebase.firestore().collection(collectionName).

query = query.where(...)
query = query.where(...)  

and with use of condition

if (condition) {
  query = query.where(...)
}  

execute query after all clause add.

query.get().then(...)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文