如何组合两个Redux Firebase云Firestore查询?
我有两个redux查询,它们从我的Firebase Firestore中获取帖子。第一个成功显示了我跟踪的人的所有帖子:如何组合两个Redux Firebase云Firestore查询?,firebase,react-native,redux,google-cloud-firestore,expo,Firebase,React Native,Redux,Google Cloud Firestore,Expo,我有两个redux查询,它们从我的Firebase Firestore中获取帖子。第一个成功显示了我跟踪的人的所有帖子: export function fetchUsersFollowingPosts(uid) { return ((dispatch, getState) => { firebase.firestore() .collection("posts") .doc(uid)
export function fetchUsersFollowingPosts(uid) {
return ((dispatch, getState) => {
firebase.firestore()
.collection("posts")
.doc(uid)
.collection("userPosts")
.orderBy("creation", "asc")
.get()
.then((snapshot) => {
const uid = snapshot.query.EP.path.segments[1];
const user = getState().usersState.users.find(el => el.uid === uid);
let posts = snapshot.docs.map(doc => {
const data = doc.data();
const id = doc.id;
return { id, ...data, user }
})
for (let i = 0; i < posts.length; i++) {
dispatch(fetchUsersFollowingLikes(uid, posts[i].id))
}
dispatch({ type: USERS_POSTS_STATE_CHANGE, posts, uid })
})
})
}
这里是我目前列出的我关注的人的用户。但是我如何将它们结合起来,以便在一个单一的平面列表中显示我的帖子和我关注的人的帖子呢
function Feed(props) {
useStatusBar('dark-content');
const [posts, setPosts] = useState([]);
const [refreshing, setRefreshing] = useState(false)
useEffect(() => {
if (props.usersFollowingLoaded == props.following.length && props.following.length !== 0) {
props.feed.sort(function (x, y) {
return y.creation.toDate() - x.creation.toDate();
})
setPosts(props.feed);
setRefreshing(false)
}
}, [props.usersFollowingLoaded, props.feed])
return (
<View style={styles.background}>
{posts.length > 0 ?
<View style={styles.containerGallery}>
<FlatList
refreshControl={
<RefreshControl
refreshing={refreshing}
tintColor="white"
onRefresh={() => {
setRefreshing(true);
props.reload()
}}
/>
}
showsVerticalScrollIndicator={false}
numColumns={1}
horizontal={false}
data={posts}
renderItem={({ item }) => (
<View style={styles.containerImage}>
<Card title={item.title} onPress={() => props.navigation.navigate(routes.GOOD_STUFF_DETAIL, { item: item, postId: item.id, uid: item.user.uid, user: item.user,})} showLike={true} author={"Recommended by " + item.user.name} likeItem={item} likeCount={item.likesCount} icon={categories.categories[item.categoryID].icon} timeStamp={timeDifference(new Date(), item.creation.toDate())}/>
</View>
)}
/>
</View>
: <NothingHere title="Follow friends" text="To see their Good Stuff here" icon="search" color="white"/> }
</View>
)
}
const mapStateToProps = (store) => ({
currentUser: store.userState.currentUser,
following: store.userState.following,
feed: store.usersState.feed,
usersFollowingLoaded: store.usersState.usersFollowingLoaded,
})
const mapDispatchProps = (dispatch) => bindActionCreators({ reload }, dispatch);
export default connect(mapStateToProps, mapDispatchProps)(Feed);
功能提要(道具){
useStatusBar(“暗内容”);
const[posts,setPosts]=useState([]);
const[refreshing,setRefreshing]=useState(false)
useffect(()=>{
if(props.usersFollowingLoaded==props.following.length&&props.following.length!==0){
props.feed.sort(函数(x,y){
返回y.creation.toDate()-x.creation.toDate();
})
设置柱(道具进给);
设置刷新(错误)
}
},[props.usersFollowingLoaded,props.feed])
返回(
{posts.length>0?
}
showsVerticalScrollIndicator={false}
numColumns={1}
水平={false}
数据={posts}
renderItem={({item})=>(
props.navigation.navigate(routes.GOOD_STUFF_DETAIL,{item:item,postId:item.id,uid:item.user.uid,user:item.user,})showLike={true}author={“推荐者”{item.user.name}likeItem={item}likeCount icon={categories.categorid.[item.categoryID].icon}时间戳={时差(新日期(),item.creation.toDate
)}
/>
: }
)
}
常量mapStateToProps=(存储)=>({
currentUser:store.userState.currentUser,
following:store.userState.following,
提要:store.usersState.feed,
usersFollowingLoaded:store.usersState.usersFollowingLoaded,
})
const-mapDispatchProps=(dispatch)=>bindActionCreators({reload},dispatch);
导出默认连接(mapStateToProps、mapDispatchProps)(提要);
以下是我的数据结构:
谢谢你的阅读 也许我误解了数据库结构,但似乎不可能同时使用这两种查询。从我看到的数据库结构中,您希望检索userB的帖子,其中您(userA)给出了一个“like”。为了获得这些信息,您需要扫描路径
posts/{userUID}/userPosts/{docId}/likes
。由于查询扫描不同的集合范围,我看不到混合它们的方法
另外,为了论证的目的,让我们假设您已经有了一个列表,其中包含您关注的人的用户UID。然后,更接近所需行为的Firestore查询功能被删除。考虑到数据库的结构:
posts
uid1
userPosts
doc1
doc2
uid2
userPosts
docX
docY
本质上,集合组查询是一种同时跨所有userPosts
集合进行查询的方法。如果每个文档都有作者ID作为字段,则可以执行以下操作:
db.collectionGroup("userPosts").where("uid", "in", ListOfFollowedUsers)
这并不能完全解决问题,因为operator子句中的限制为10个值,因此最多可以为10个后续用户应用它
总的来说,我建议将查询分开,并将它们合并到应用程序代码中。基于您的数据,并且知道Firestore上的查询是多么有限,您需要在客户端上进行合并
我要做的是将列表保留在redux上,并在reducer上处理合并。你只需要监听这两个动作,然后将它们合并为应用程序上的一个数组
如果您想避免用户看到部分数据(例如,您显示自己的帖子,然后另一次刷新只是添加您的关注者帖子,因此UI将改变),您可能希望在加载数据时保留两个标志(布尔值),如果两个列表都未加载,则显示微调器
除非你想重构你的代码,否则很多合并都发生在客户端上
另外,在节点端,我不会像在第一个循环中那样在for循环中调度某些内容,因为如果这触发了firestore请求,那么它可能会很快变得非常昂贵。您能否解释likes
子集合的结构和/或添加fetchUsersFollowingLikes
函数的代码?我还想了解你为什么想加入这些问题。谢谢你询问@Happy Monad。我添加了一个likes子集合的图像——它们只是一个用户ID列表。我想把这些问题放在一起,在一个列表中显示我关注的人的帖子以及我自己的帖子。谢谢你如此周到和深思熟虑的回答。听起来我需要在应用程序代码中合并它们。我在下面包含了应用程序代码。如果有任何帮助,我们将不胜感激@TomWicks从代码中我相信feed
是一个用户关注帖子的列表。如果你的帖子中有另一个列表,那么:从商店导入列表,连接它们,按日期排序,就这样了。
db.collectionGroup("userPosts").where("uid", "in", ListOfFollowedUsers)