如何根据条件添加火中查询中的状况?
我正在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需引用
集合
。让Query = firebase.firestore()。collection(collectionName)
。并使用条件
执行查询后,所有子句添加。
just take reference of
collection
.let query = firebase.firestore().collection(collectionName)
.and with use of condition
execute query after all clause add.