firebase 无法从firestore访问数据

krcsximq  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(143)

因此,我想根据我的react native应用程序中的登录用户访问配置文件名称。这里的问题是,即使我可以访问suer文档,我也无法访问其中的数据。

  1. import React, { useState, useEffect } from 'react';
  2. import { View, Text, StyleSheet } from 'react-native';
  3. import { app, auth } from '../firebaseconfig';
  4. import { getFirestore, doc, getDoc } from 'firebase/firestore';
  5. const db = getFirestore(app);
  6. const MainPage = () => {
  7. const [userName, setUserName] = useState('');
  8. useEffect(() => {
  9. const fetchUserData = async () => {
  10. try {
  11. const user = auth.currentUser;
  12. console.log('current user UID is:', user.uid);
  13. if (user) {
  14. const userDocRef = doc(db, 'users', user.uid);
  15. console.log('The doc is:', userDocRef);
  16. const userDocSnapshot = await getDoc(userDocRef);
  17. console.log('User document:', userDocSnapshot);
  18. if (userDocSnapshot) {
  19. console.log('User document exists:', userDocSnapshot.data());
  20. // Check if userData and userData.name exist before setting the state
  21. const userData = userDocSnapshot.data();
  22. if (userData && 'name' in userData) {
  23. setUserName(userData.name);
  24. console.log('Name property found:', userData.name);
  25. } else {
  26. console.log('Name property not found in user data');
  27. }
  28. } else {
  29. console.log('User document does not exist in Firestore');
  30. }
  31. }
  32. } catch (error) {
  33. console.error('Error fetching user data:', error);
  34. }
  35. };
  36. fetchUserData();
  37. }, []);
  38. return (
  39. <View style={styles.container}>
  40. <Text style={styles.text}>Hello, {userName}</Text>
  41. </View>
  42. );
  43. };
  44. const styles = StyleSheet.create({
  45. container: {
  46. flex: 1,
  47. justifyContent: 'center',
  48. alignItems: 'center',
  49. },
  50. text: {
  51. fontSize: 20,
  52. fontWeight: 'bold',
  53. },
  54. });
  55. export default MainPage;

日志

  1. LOG current user UID is: 4sXptgVpAgUSh8jILHuv45R8fiz1
  2. LOG The doc is: {"_key": {"path": {"len": 2, "offset": 0, "segments": [Array]}}, "converter": null, "firestore": {"app": [FirebaseAppImpl], "databaseId": [DatabaseId], "settings": [FirestoreSettingsImpl]}, "type": "document"}
  3. LOG User document: {"_converter": null, "_document": null, "_firestore": {"app": [FirebaseAppImpl], "databaseId": [DatabaseId], "settings": [FirestoreSettingsImpl]}, "_firestoreImpl": {"app": [FirebaseAppImpl], "databaseId": [DatabaseId], "settings": [FirestoreSettingsImpl]}, "_key": {"path": {"len": 2, "offset": 0, "segments": [Array]}}, "_userDataWriter": {"firestore": [Object]}, "metadata": {"fromCache": false, "hasPendingWrites": false}}
  4. LOG User Data: undefined
  5. LOG Name property not found in user data

1.重新配置Firestore规则,以确保它们允许基于用户的UID进行读取访问。
1.控制台已记录代码以检查为什么我无法访问数据。

wi3ka0sx

wi3ka0sx1#

调用getDocalways会导致DocumentSnapshot,所以这个检查永远不会失败:

  1. if (userDocSnapshot) {

如果要检查快照是否属于现有文档,请调用其exists方法:

  1. if (userDocSnapshot.exists()) {

这将修复该行为,并可能显示用户不存在文档。要进一步解决这个问题,请仔细比较您试图加载的文档的路径与数据库中的实际路径。

相关问题