【问题标题】:DocumentReference.get(<Field>) Not workingDocumentReference.get(<Field>) 不工作
【发布时间】:2019-11-11 05:52:34
【问题描述】:

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

This is the data I am trying to read.

上面写着here 我可以做.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');
      }
    });
  };

【问题讨论】:

    标签: javascript firebase react-native google-cloud-firestore


    【解决方案1】:

    我认为你想要的逻辑类似于:

    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&lt;DocumentSnapshot&gt;
    • DocumentSnapshot.get() -> 字段值

    另见:

    【讨论】:

    • 如何获取 DocumentSnapshot?我需要DocumentReference.get().then(DocumentSnapshot =&gt; {...})吗?
    • 道歉......我忽略了我的原始文本包含尖括号,这些尖括号未在响应中显示。对 DocumentReference.get() 的调用返回一个 JavaScript 承诺,该承诺在解决后返回一个 DocumentSnapshot。所以是的,你理解正确......在你上面的评论中你完全正确。
    • 非常感谢。我一直对此感到沮丧。
    猜你喜欢
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 2019-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    相关资源
    最近更新 更多