Using Firestore for react function but data was reset

69 views
Skip to first unread message

Dat Dinh

unread,
Jun 14, 2022, 8:52:22 AM6/14/22
to Firebase Google Group
Hi everyone,
Has anyone here ever used Firestore to save and Realtime update react action. I have try to using it. However, when there is a large number of users accessing it at the same time, the problem is that the document of itself is reset to 0 like when it was created. Is there any way to know the cause and its fixes?

Below is the test that exists to update or create a new one :
const usersRef = db.collection(documentFirebase).doc(video_id)
        await usersRef.get()
            .then((docSnapshot) => {
                if (!docSnapshot.exists) {
                    // Insert first document Data
                    usersRef.set({
                        reaction_love: 0,
                        reaction_laugh: 0,
                        reaction_cry: 0
                    })
                }

                // Show data at Home Ratting
                usersRef.onSnapshot((doc) => {
                    Apps.disPlayNumberAtHome(doc.data());
                });

            }).catch((error) => {
                console.log(error)
            });

Ashish

unread,
Jun 15, 2022, 8:37:33 AM6/15/22
to Firebase Google Group
The way you wrote the code for a set or created a reaction to a specific video is wrong.

const usersRef = db.collection(documentFirebase).doc(video_id);
As you can say above line will give you the proper reference to database path.

const userDoc = await usersRef.get();
Now when you using async/await you should put the retrieved data in a variable to read that later, async/await does not support .then()

if (userDoc.data() === undefined) {

    usersRef.set({
          reaction_love: 0,
          reaction_laugh: 0,
          reaction_cry: 0
    });
}
Apps.disPlayNumberAtHome(doc.data());

This will help you to get the data and show it to the screen.

For further details about async/await, If you still facing an issue, ping me. Thanks

Dat Dinh

unread,
Jun 20, 2022, 8:36:08 AM6/20/22
to Firebase Google Group
Hi, Ashish

Thank you in advance. I am forced to use to have real time result updated in view. So, I have try to update same like this:

const usersRef = db.collection(documentFirebase).doc(video_id)
const userDoc = await usersRef.get()
        if (userDoc.data() === undefined) {
            // Insert first document Data
            usersRef.set({
                reaction_love: 0,
                reaction_laugh: 0,
                reaction_cry: 0
            })
        }
        // Show data at Home
        usersRef.onSnapshot((doc) => {
            Apps.disPlayNumberAtHome(doc.data());
        });

Do you think it's ok like that?
Reply all
Reply to author
Forward
0 new messages