count the number of likes using react native and firebase firestore

Viewed 907

This is the firestore data structure where I was able to store the ids of users who liked it but not able to count the total number of likes.

enter image description here

I am trying to count and display the total number of likes for a particular post, whereas I was able to store the interactions on the post but I want the total like count as of Insta or FB posts.

 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>
1 Answers

You can use FieldValue.increment() for that. Applying to your code all you have to do is this:

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)
                 });
             })
}
Related