Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.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
Firebase react-native无法从firestore获取要在react-native应用程序中显示的文档字段_React Native_Google Cloud Firestore_Expo - Fatal编程技术网

Firebase react-native无法从firestore获取要在react-native应用程序中显示的文档字段

Firebase react-native无法从firestore获取要在react-native应用程序中显示的文档字段,react-native,google-cloud-firestore,expo,React Native,Google Cloud Firestore,Expo,我对本地和firestore都不熟悉 背景: Firestore有一个文档“用户”,而“位置”是一个字段 要求:我希望在react原生应用程序中显示一个用户/登录用户的位置。 我已经编写了以下代码,但是登录后位置字段为空。 请指出我错在哪里 建造师(道具){ 超级(道具); 此.state={ 档案用户:[], profileuserlocation:“” }; } componentDidMount(){ firebase.firestore().collection('users').d

我对本地和firestore都不熟悉

背景: Firestore有一个文档“用户”,而“位置”是一个字段

要求:我希望在react原生应用程序中显示一个用户/登录用户的位置。 我已经编写了以下代码,但是登录后位置字段为空。 请指出我错在哪里


建造师(道具){
超级(道具);
此.state={
档案用户:[],
profileuserlocation:“”
};
}
componentDidMount(){
firebase.firestore().collection('users').doc(firebase.auth().currentUser.uid).get().then(snapshot=>{
让profileuserlocation=snapshot.data().Location;
this.setState({profileuserlocation:snapshot.data().Location});
});
}
render(){
const profileuserlocation=this.state.profileuserlocation;
返回(
{this.state.profileuserlocation}
)

我知道有点晚了,但以防万一你永远都找不到答案

获取当前用户uid的标准方法是:

      firebase.auth().onAuthStateChanged(user => {
        if (user) {
          firebaseUser = {
             uid: user.uid,
            name: user.displayName,//this and the email can also be got from firebaseUser 
            email: user.email
          };
          this.setState({ uid: firebaseUser.uid });
        }
      });  
然后,如果您只想显示文档中的字段,则不需要使用snapshot—如果您想继续监视字段中的更改并以不同的方式使用,则确实需要使用snapshot。如果您只想检索一次字段,则使用:

const docRef = firebase.firestore().collection('users').doc(this.state.uid)

this.docRef
    .doc(this.state.uid)
    .get().then(function (doc) {
        if (doc.exists) {

//useful ES6 'destructuring' technique for if there are many fields you are 
//saving
            const {
                Location
            } = doc.data();

            this.setState({
                Location
            })

//or for one field you can use "this.setState({Location:doc.data().Location})"

          } else {
            // doc.data() will be undefined in this case
            console.log("No such document!");
        }
    }).catch(function (error) {
        console.log("Error getting document:", error);
    });


您可以在文档中获得一些很好的示例:


希望这对某人有所帮助…

我知道有点晚了,但以防万一你永远都找不到答案

获取当前用户uid的标准方法是:

      firebase.auth().onAuthStateChanged(user => {
        if (user) {
          firebaseUser = {
             uid: user.uid,
            name: user.displayName,//this and the email can also be got from firebaseUser 
            email: user.email
          };
          this.setState({ uid: firebaseUser.uid });
        }
      });  
然后,如果您只想显示文档中的字段,则不需要使用snapshot—如果您想继续监视字段中的更改并以不同的方式使用,则确实需要使用snapshot。如果您只想检索一次字段,则使用:

const docRef = firebase.firestore().collection('users').doc(this.state.uid)

this.docRef
    .doc(this.state.uid)
    .get().then(function (doc) {
        if (doc.exists) {

//useful ES6 'destructuring' technique for if there are many fields you are 
//saving
            const {
                Location
            } = doc.data();

            this.setState({
                Location
            })

//or for one field you can use "this.setState({Location:doc.data().Location})"

          } else {
            // doc.data() will be undefined in this case
            console.log("No such document!");
        }
    }).catch(function (error) {
        console.log("Error getting document:", error);
    });


您可以在文档中获得一些很好的示例:


希望这对某人有所帮助…

非常感谢。我今天将尝试并回复。非常欢迎您…如果答案对您有效,请在我的答案旁边的灰色勾上勾选“接受”它!谢谢!非常感谢。我今天将尝试并回复。非常欢迎您…如果答案对您有效,请不要客气o在我答案旁边的灰色勾号上勾选“接受”它!谢谢!