Home > Blockchain >  TypeError: null is not an object (evaluating 'documentSnapshot.exists')
TypeError: null is not an object (evaluating 'documentSnapshot.exists')

Time:06-16

I have a component ShowReportCount which throws an error when I logout with auth().signOut(); It presents me with the following error:

TypeError: null is not an object (evaluating 'documentSnapshot.exists')

console.log('UID:', user.uid) returns:

**UID:** lzDbqczG5yXaMJRzHZO3VQEOD0n2

console.log('dbUser:', dbUser) returns:

**dbUser:** {"_documentPath": {"_parts": ["users", "lzDbqczG5yXaMJRzHZO3VQEOD0n2"]}, "_firestore": {"_app": {"_automaticDataCollectionEnabled": true, "_deleteApp": [Function bound deleteApp], "_deleted": false, "_initialized": true, "_name": "[DEFAULT]", "_nativeInitialized": true, "_options": [Object]}, "_config": {"ModuleClass": [Function FirebaseFirestoreModule], "hasCustomUrlOrRegionSupport": false, "hasMultiAppSupport": true, "namespace": "firestore", "nativeEvents": [Array], "nativeModuleName": [Array], "statics": [Object], "version": "10.5.1"}, "_customUrlOrRegion": undefined, "_nativeModule": {"RNFBFirestoreCollectionModule": true, "RNFBFirestoreDocumentModule": true, "RNFBFirestoreModule": true, "RNFBFirestoreTransactionModule": true, "clearPersistence": [Function anonymous], "collectionGet": [Function anonymous], "collectionOffSnapshot": [Function anonymous], "collectionOnSnapshot": [Function anonymous], "disableNetwork": [Function anonymous], "documentBatch": [Function anonymous], "documentDelete": [Function anonymous], "documentGet": [Function anonymous], "documentOffSnapshot": [Function anonymous], "documentOnSnapshot": [Function anonymous], "documentSet": [Function anonymous], "documentUpdate": [Function anonymous], "enableNetwork": [Function anonymous], "getConstants": [Function anonymous], "setLogLevel": [Function anonymous], "settings": [Function anonymous], "terminate": [Function anonymous], "transactionApplyBuffer": [Function anonymous], "transactionBegin": [Function anonymous], "transactionDispose": [Function anonymous], "transactionGetDocument": [Function anonymous], "waitForPendingWrites": [Function anonymous]}, "_referencePath": {"_parts": [Array]}, "_transactionHandler": {"_firestore": [Circular], "_pending": [Object]}}}

Can anyone provide me with a solution to this issue?

ShowReportCount.js

import React, { useContext, useState } from 'react';
import { Text, View } from 'react-native';
import firestore from '@react-native-firebase/firestore';

import styles from './ShowReportCount.styles';
import { UserContext } from '../../../context/UserContext';

const db = firestore();

export default function ShowReportCount(type) {
    const [count, setCount] = useState(0);
    const [user, setUser] = useContext(UserContext);

    console.log('UID:', user.uid);
    const dbUser = db.collection('users').doc(user.uid);
    console.log('dbUser:', dbUser);

    dbUser.onSnapshot(
        {
            includeMetadataChanges: true,
        },
        (documentSnapshot) => {
            if (documentSnapshot.exists) {
                if (type.type === 'exercise') {
                    setCount(documentSnapshot.data().exerciseCount);
                    return;
                }

                if (type.type === 'workout') {
                    setCount(documentSnapshot.data().workoutCount);
                    return;
                }
            } else {
                console.log("error: document doesn't exist");
            }
        }
    );

    return (
        <View>
            <Text style={styles.statCardNumber}>{count.toString()}</Text>
        </View>
    );
}

CodePudding user response:

This occurs if there's an error happening on the snapshot. Make sure that the user has the authenticated object on it. To catch what error is happening on your snapshot, supply an error handler as a second param to onSnapshot. See sample code below:

    dbUser.onSnapshot(
        {
            includeMetadataChanges: true,
        },
        (documentSnapshot) => {
            if (documentSnapshot.exists) {
                if (type.type === 'exercise') {
                    setCount(documentSnapshot.data().exerciseCount);
                    return;
                }

                if (type.type === 'workout') {
                    setCount(documentSnapshot.data().workoutCount);
                    return;
                }
            } else {
                console.log("error: document doesn't exist");
            }
        }, (error) => {
           // Catch errors from `onSnapshot`.
           console.log(error);
    });

For more information, checkout Handle listen errors.

  • Related