Firebase Firestore和Cloud Functions显示错误的读取,写入和删除数字?

时间:2019-02-17 11:14:20

标签: firebase google-cloud-firestore google-cloud-functions

我有这样的消防站结构:

users(collection):
    -userID_1(document):
        email: email1@gmail.com
        name: userName_1
        photo: photoUrl
        uid: userID_1
        followersNum: 1
        followingNum: 0
        questionsNum: 0
    -userID_2(document):
        email: email2@gmail.com
        name: userName_2
        photo: photoUrl
        uid: userID_2
        followersNum: 0
        followingNum: 1
        questionsNum: 0
followingFollowersPoints(collection):
    -userID_2(document):
        -following(collection):
            -userID_1(document)
                -points: 1

执行任何操作之前,我的Firestore和Cloud Functions编号: enter image description here enter image description here 今天(2月17日)有0个clud函数调用和0个读,写,删除。 然后,我用firebase deploy --only functions:followUser部署了云功能,然后打开firestore用法并获得以下数字: enter image description here 我没有打开我的应用程序,也没有接触过Firestore控制台,我只是重新打开了Firestore中的“使用情况”页面,这怎么可能? 然后,在测试云功能之前,我手动更新了两个用户文档并删除了以下集合中的一个文档,因此我更新了两个文档中的一个字段并删除了一个文档,但是得到了以下数字: 这就是Firestore的变化方式:

As you can see I only updated two field, from value 1 to value 0 and deleted one document in following colelction
users(collection):
    -userID_1(document):
        email: email1@gmail.com
        name: userName_1
        photo: photoUrl
        uid: userID_1
        followersNum: 0
        followingNum: 0
        questionsNum: 0
    -userID_2(document):
        email: email2@gmail.com
        name: userName_2
        photo: photoUrl
        uid: userID_2
        followersNum: 0
        followingNum: 0
        questionsNum: 0
followingFollowersPoints(collection):
    -userID_2(document):
        -following(collection):

这是Firestore的用法:  enter image description here

然后,我在以下集合中手动添加同一文档,该文档将调用云功能,而我的云功能将更新两个文档中的一个字段。添加该文档后,这是我的firestore结构:

users(collection):
        -userID_1(document):
            email: email1@gmail.com
            name: userName_1
            photo: photoUrl
            uid: userID_1
            **followersNum: 1 -CloudFunction updated this field**
            followingNum: 0
            questionsNum: 0
        -userID_2(document):
            email: email2@gmail.com
            name: userName_2
            photo: photoUrl
            uid: userID_2
            followersNum: 0
            **followingNum: 1 -CloudFunction updated this field**
            questionsNum: 0
    followingFollowersPoints(collection):
        -userID_2(document):
            -following(collection):
                **-userID_1(document) - I added this document that invokes fucntion**
                    -points: 1

这些是调用云函数后的使用次数: enter image description here enter image description here 然后只是为了确保数字已更新,我刷新了浏览器并获得了以下数字: enter image description here 当我写这篇文章大约15分钟时,读取次数最多达到177!?!?

当我在stackoverflow上写这个时,我还没有接触过Firestore或应用程序。

这是我的CloudFunction代码:

admin.initializeApp();
 const db = admin.firestore();

 export const followUser = functions.firestore
    .document('followingFollowersPoints/{myUserId}/following/{followingUserId}')
    .onCreate((snap, context) => {

        const myUserId = context.params.myUserId               //update my following count
        const followingUserId = context.params.followingUserId //update his followers count
        const myUserDocRef = db.collection('users').doc(myUserId)
        const followingUserDocRef = db.collection('users').doc(followingUserId)
        return db.runTransaction(transaction => {
            return transaction.getAll(myUserDocRef, followingUserDocRef) //**********THESE ARE TWO READS*****************
                    .then(docs => {

                        const myUserData = docs[0].data()
                        const followingUserData = docs[1].data()

                        if(myUserData != null && followingUserData != null){
                            //since I am following someone - increase my followingNum
                            const newFollowingCount = myUserData.followingNum + 1
                            //then increse his followersNum
                            const newFollowersCount = followingUserData.followersNum + 1

                            //update my followingNum field in user document
                            //*******THIS IS ONE WRITE*********
                            transaction.update(myUserDocRef, {
                                followingNum : newFollowingCount
                            });
                            //*******THIS IS ONE WRITE*********
                            //update friends followersNum
                            transaction.update(followingUserDocRef, {
                                followersNum : newFollowersCount
                            });
                        }
                    });
          }).then(() => console.log('Transaction succeeded'))  
    });

我真的不知道我怎么得到这个数字,只是没有任何意义。我没有任何其他云功能可以通过第一个云功能的更改来调用。当我在云功能中只有两次读取时,177次读取实在太多了。

0 个答案:

没有答案