TypeError:NULL不是对象(评估' documentsnapshot.exists')

发布于 2025-02-06 22:14:21 字数 3568 浏览 1 评论 0原文

我有一个组件ShowReportCount,当我使用auth()。签名();登录时会引发错误,它给我带来以下错误:

typeError:null不是对象(评估 'documentsnapshot.exists')

console.log('uid:',user.uid)返回:

**UID:** lzDbqczG5yXaMJRzHZO3VQEOD0n2

console.log('dbuser:',dbuser)返回:

**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]}}}

任何人都可以为我提供解决这个问题的解决方案?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

゛时过境迁 2025-02-13 22:14:22

如果在快照上发生错误,则会发生这种情况。确保用户在其上具有认证的对象。要查看快照上发生的错误,请提供错误处理程序作为第二个参数onsnapshot。请参阅下面的示例代码:

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

有关更多信息,请查看 >。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文