首页 > 解决方案 > 文档参考.get() 不工作

问题描述

我只是想找到这个布尔值,如果它是真的,那么它将执行一个代码。否则,它将执行另一个代码。我似乎无法在 Firebase 中读取此布尔值。

这是我要读取的数据。

在这里说我可以做,.get(fieldPath)但我可能做错了。Firebase 文档真的很糟糕。哈哈

checkIfLoggedIn = () => {
    firebase.auth().onAuthStateChanged(user => {
      if (user) {
        const uid = firebase.auth().currentUser.uid;
        const db = firebase.firestore();
        db.collection('users')
          .doc(uid)
          .get('uid.isStore')
          .then(snapshot => {
            // Trying to find how to do this
            console.log(snapshot);
          });
        this.props.navigation.navigate('mainNav');
      } else {
        this.props.navigation.navigate('signup');
      }
    });
  };

标签: javascriptfirebasereact-nativegoogle-cloud-firestore

解决方案


我认为您想要的是类似于以下的逻辑:

db.collection('users')
  .doc(uid)
  .get()
  .then(documentSnapshot => {
    let isStore = documentSnapshot.get('isStore');
    // Value of isStore here ... 
  });

链条是:

  • db.collection->CollectionReference
  • CollectionReference.doc()->DocumentReference
  • DocumentReference.get()->Promise<DocumentSnapshot>
  • DocumentSnapshot.get()-> 字段值

也可以看看:


推荐阅读