TypeError:NULL不是对象(评估' documentsnapshot.exists')
我有一个组件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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果在
快照
上发生错误,则会发生这种情况。确保用户
在其上具有认证的对象。要查看快照上发生的错误,请提供错误处理程序作为第二个参数onsnapshot
。请参阅下面的示例代码:有关更多信息,请查看 >。
This occurs if there's an error happening on the
snapshot
. Make sure that theuser
has the authenticated object on it. To catch what error is happening on your snapshot, supply an error handler as a second param toonSnapshot
. See sample code below:For more information, checkout Handle listen errors.