首页 > 解决方案 > 如何在一个查询中结合“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)
            }
            
}

标签: firebasereact-nativegoogle-cloud-firestorereact-redux

解决方案


您的第二个查询包括.orderBy('id',"desc"),第一个不包括。它们不是同一个查询,而且顺序也不相同。


推荐阅读