firebase - 如何在一个查询中结合“where”、“starAt”和“limit” [FIREBASE,REACT NATIVE,REDUX]
问题描述
我正在尝试构建一个无限滚动,这些是我正在使用的函数,我有两个正在使用的函数fetchDistrubutors()
, fetchMoreDistrubutors()
第一个函数按预期工作,但第二个查询在 onSnpashot 中返回 null:初始获取(工作正常)
async fetchDistrubutors(arg,state){
try {
const fetch_limit = state.distrubutor.fetch_limit
const distrubutorsResponse= await firestore()
.collection('users')
.where('type','==','DISTRUBUTOR')
.limit(fetch_limit)
distrubutorsResponse.onSnapshot(res=>{
if(res.docs.length)
{
const distrubtors = res.docs.map(doc=>doc.data())
dispatch.distrubutor.fetcheddistrubutors({
distrubutors:distrubtors,
last_visible : res.docs[res.docs.length-1].id
})
}
})
} catch (error) {
console.log(error)
}
},
这是我在LoadMore
函数中遇到问题的地方,查询 res 返回 null,我不知道我在做什么错
async fetchMoreDistrubutors(arg,state){
try {
const fetch_limit = state.distrubutor.fetch_limit
const last_visible = state.distrubutor.last_visible
console.log({last_visible})
console.log({fetch_limit})
const moreDstrubutorsResponse= await firestore()
.collection('users')
.where('type','==','DISTRUBUTOR')
.orderBy('id',"desc")
.startAt(last_visible)
.limit(fetch_limit)
moreDstrubutorsResponse.onSnapshot(res=>{
if(res == undefined) return
console.log(res)
if(res.docs.length)
{
const PrevDistrubtors = state.distrubutor.distrubutors
const newDistrubtors = res.docs.map(doc=>doc.data())
dispatch.distrubutor.fetcheddistrubutors({
distrubutors : [...PrevDistrubtors,...newDistrubtors],
last_visible : res.docs[res.docs.length -1].id
})
}
})
} catch (error) {
console.log(error)
}
}
解决方案
您的第二个查询包括.orderBy('id',"desc")
,第一个不包括。它们不是同一个查询,而且顺序也不相同。
推荐阅读
- node.js - 我如何在 gulp 4.0 版中重写这个 gulpfile.js
- javascript - 如果已经使用同一用户控件的不同实例上传了同名文件,如何限制从用户控件的一个实例上传文件
- postgresql-11 - 如何在 postgres 中使用序列名获取表名和列名?
- python - 在 Azure Functions 中输入多个 blob
- if-statement - 将 if/else 的主体留空的约定
- symfony4 - 如何在 api-platform 的 dto 中返回输出类的数据实例
- python - Sklearn 中 Isomap 的示例扩展
- go - 如何在 Go 中解析 RFC3339?
- javascript - jsVectorMap 渲染太小并破坏布局
- oracle - Oracle字符串操作根据分隔符排除特定字符