Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript React Native Undefined不是对象(计算';userData.id)_Javascript_Reactjs_Firebase_React Native_Compiler Errors - Fatal编程技术网

Javascript React Native Undefined不是对象(计算';userData.id)

Javascript React Native Undefined不是对象(计算';userData.id),javascript,reactjs,firebase,react-native,compiler-errors,Javascript,Reactjs,Firebase,React Native,Compiler Errors,我对react native是新手,我有一个个人项目,我试图从Firestore云获取数据,但我在屏幕更改时不断收到此错误。 当我注释掉数据库代码时,它工作正常,所以我想知道是什么原因造成的 我的代码 从“React”导入React; 从“@react native firebase/auth”导入身份验证; 从“@react native firebase/firestore”导入firestore; const ProfileStackScreen=({navigation})=>{ Rea

我对react native是新手,我有一个个人项目,我试图从Firestore云获取数据,但我在屏幕更改时不断收到此错误。 当我注释掉数据库代码时,它工作正常,所以我想知道是什么原因造成的

我的代码

从“React”导入React;
从“@react native firebase/auth”导入身份验证;
从“@react native firebase/firestore”导入firestore;
const ProfileStackScreen=({navigation})=>{
React.useffect(()=>{
const usr=auth().currentUser;
setuserData(prev=>{
返回{…prev,uid:usr.uid};
});
}, []);
const userRef=firestore().collection(“用户”);
const snapshot=userRef
.where(“uid”,“==”,userData.uid)
.onSnapshot()
.then(console.log(uid))
.catch(错误=>{
警报。警报(错误消息);
});
const[userData,setuserData]=React.useState({
uid:“
//其他领域去这里
});
返回(
{userData.uid}
);
};
导出默认配置文件StackScreen;
您可以尝试以下代码

import React from 'react';
import auth from '@react-native-firebase/auth';
import firestore from '@react-native-firebase/firestore';

const ProfileStackScreen = ({ navigation }) => {

  React.useEffect(() => {
    const usr = auth().currentUser;
    setuserData((prev)=>{
      return {...prev,uid: usr.uid};
    });
  }, []);
 React.useEffect(() => {
  fetchdata()
  }, [userData]);// Once userData value has been updated then only call fetchData()

 const fetchdata = ()=>{
  const userRef = firestore().collection('users').doc(userData.uid).get()
            .then(function (doc) {
              if (doc.exists) {
                console.log("Document found!");
               console.log(doc.data())
              } else {
               
                console.log("No such document!");
                
              }
            });
    }

  

  const [userData, setuserData] = React.useState({
    uid: '',
  // other field go here
 
    });
  return (
    <View>
          <Text>{userData.uid}</Text>
    </View>
  );
};

export default ProfileStackScreen;
从“React”导入React;
从'@react native firebase/auth'导入身份验证;
从“@react native firebase/firestore”导入firestore;
const ProfileStackScreen=({navigation})=>{
React.useffect(()=>{
const usr=auth().currentUser;
setuserData((上一个)=>{
返回{…prev,uid:usr.uid};
});
}, []);
React.useffect(()=>{
fetchdata()
},[userData]);//更新userData值后,只调用fetchData()
常量fetchdata=()=>{
const userRef=firestore().collection('users').doc(userData.uid).get()
.然后(功能(文档){
如果(文件存在){
log(“找到文档!”);
console.log(doc.data())
}否则{
log(“没有这样的文档!”);
}
});
}
const[userData,setuserData]=React.useState({
uid:“”,
//其他领域去这里
});
返回(
{userData.uid}
);
};
导出默认配置文件StackScreen;

@Maheshvirus是对的。但我认为您已尝试在
userData.uid
不为空时获取数据

如果你想找这样的方法,就试试这个方法

import React from 'react';
import auth from '@react-native-firebase/auth';
import firestore from '@react-native-firebase/firestore';

const ProfileStackScreen = ({ navigation }) => {

  React.useEffect(() => {
    const usr = auth().currentUser;
    setuserData((prev)=> {
      return {...prev,uid: usr.uid};
    });
  }, []);
    
    React.useEffect(() => {
       if(userData.uid !== ''){
          getData()
       }
    }, [userData]);

    const getData = () => {
       firestore()
       .collection('users');
       .where('uid', '==', userData.uid)
       .onSnapshot()
       .then(() => {
         console.log(uid)
       })
       .catch((error)=> {
         Alert.alert(error.message);
       });
    }

  const [userData, setuserData] = React.useState({
    uid: '',
  // other field go here
 
    });
  return (
    <View>
          <Text>{userData.uid}</Text>
    </View>
  );
};

export default ProfileStackScreen;
从“React”导入React;
从'@react native firebase/auth'导入身份验证;
从“@react native firebase/firestore”导入firestore;
const ProfileStackScreen=({navigation})=>{
React.useffect(()=>{
const usr=auth().currentUser;
setuserData((上一个)=>{
返回{…prev,uid:usr.uid};
});
}, []);
React.useffect(()=>{
如果(userData.uid!=''){
getData()
}
},[userData]);
常量getData=()=>{
firestore()
.集合(“用户”);
.where('uid','=',userData.uid)
.onSnapshot()
.然后(()=>{
控制台日志(uid)
})
.catch((错误)=>{
警报。警报(错误消息);
});
}
const[userData,setuserData]=React.useState({
uid:“”,
//其他领域去这里
});
返回(
{userData.uid}
);
};
导出默认配置文件StackScreen;

您是否正在控制台中获取userData.uid?为什么不将所有DB逻辑移到效果中,而不是
。where(“uid”),“==”,userData.uid)
使用
。where(“uid”),“==”,usr.uid)
(因为状态还没有更新)?哦,我明白你的意思,我会试试看。我只是需要更好地理解useffect钩子