首页 > 解决方案 > 使用 react native 和 firebase firestore 计算喜欢的数量

问题描述

这是 firestore 数据结构,我可以在其中存储喜欢它的用户的 ID,但无法计算喜欢的总数。

在此处输入图像描述

我正在尝试计算并显示特定帖子的点赞总数,而我能够将互动存储在帖子上,但我希望总点赞数与 Insta 或 FB 帖子相同。

 const onLikePress = (userId, postId) => {
        firebase.firestore()
            .collection("posts")
            .doc(userId)
            .collection("userPosts")
            .doc(postId)
            .collection("likes")
            .doc(firebase.auth().currentUser.uid)
            .set({})
    }

const onDislikePress = (userId, postId) => {
    firebase.firestore()
        .collection("posts")
        .doc(userId)
        .collection("userPosts")
        .doc(postId)
        .collection("likes")
        .doc(firebase.auth().currentUser.uid)
        .delete()
}

<View style={{flexDirection:"row" , justifyContent:"space-around" , marginTop:4}}>
                            { item.currentUserLike ?
                                (
                                    <TouchableNativeFeedback 
                                    onPress={() => onDislikePress(item.user.uid, item.id)}
                                    >
                                        <MaterialCommunityIcons name="heart" size={24} color="blue" />
                                    </TouchableNativeFeedback>
                                )
                                :
                                (
                                    <TouchableNativeFeedback 
                                    onPress={() => onLikePress(item.user.uid, item.id)} 
                                    >
                                        <MaterialCommunityIcons name="heart-outline" size={24} color="black" />
                                    </TouchableNativeFeedback>
                                  
                                )
                            }
                            <TouchableOpacity  onPress={() => props.navigation.navigate('Comment', { postId: item.id, uid: item.user.uid })}>
                                 <MaterialCommunityIcons name="comment-text-outline" size={24} color="black" />
                            </TouchableOpacity>
    
                                <TouchableOpacity onPress={ () => onShare (item.downloadURL)}>
                                     <MaterialCommunityIcons name="share-outline" size={24} color="black" />
                                </TouchableOpacity>
                            </View>
                            <Text>{item.likesCount}</Text>

                        </View>

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

解决方案


你可以使用FieldValue.increment()它。应用到您的代码中,您所要做的就是:

const onLikePress = (userId, postId) => {
    conts userPosts = firebase.firestore()
                              .collection("posts")
                              .doc(userId)
                              .collection("userPosts")
                              .doc(postId);
        
    userPosts.collection("likes")
             .doc(firebase.auth().currentUser.uid)
             .set({})
             .then(() => {
                 userPosts.update({
                     likesCont: firebase.firestore.FieldValue.increment(1)
                 });
             })
}

const onDislikePress = (userId, postId) => {
    conts userPosts = firebase.firestore()
                            .collection("posts")
                            .doc(userId)
                            .collection("userPosts")
                            .doc(postId);
        
    userPosts.collection("likes")
             .doc(firebase.auth().currentUser.uid)
             .delete()
             .then(() => {
                 userPosts.update({
                     likesCont: firebase.firestore.FieldValue.increment(-1)
                 });
             })
}

推荐阅读