最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firebase - doc exists but doc.exists returns false - Stack Overflow

programmeradmin0浏览0评论

I am trying to test whether a document exists with title uid. I have confirmed that the document exists in Firestore within the users collection, but doc.exists always returns false. Note that my document uid contains only collections (no fields).

  firebase.auth().onAuthStateChanged((user) => {
          if (user) {
            // User is signed in.
            var uid = user.uid
            db.collection("users").doc(uid)
              .get()
              .then((doc) => {
                console.log(doc)
                if (doc.exists) {
                  console.log('returning user')
                } else {
                  console.log('new user')
                }
            }).catch(function(error) {
                console.log("Error getting document:", error);
            });
          } else {
            // User is signed out.
            // ...
          }

I am trying to test whether a document exists with title uid. I have confirmed that the document exists in Firestore within the users collection, but doc.exists always returns false. Note that my document uid contains only collections (no fields).

  firebase.auth().onAuthStateChanged((user) => {
          if (user) {
            // User is signed in.
            var uid = user.uid
            db.collection("users").doc(uid)
              .get()
              .then((doc) => {
                console.log(doc)
                if (doc.exists) {
                  console.log('returning user')
                } else {
                  console.log('new user')
                }
            }).catch(function(error) {
                console.log("Error getting document:", error);
            });
          } else {
            // User is signed out.
            // ...
          }
Share Improve this question edited Jan 2, 2018 at 23:29 Frank van Puffelen 600k85 gold badges889 silver badges859 bronze badges asked Jan 2, 2018 at 22:15 astrojams1astrojams1 1,5912 gold badges16 silver badges29 bronze badges 2
  • does your console.log(doc) print anything? – Andrew L Commented Jan 2, 2018 at 22:21
  • Yes it prints a documentSnapshot object with exists property set to false – astrojams1 Commented Jan 2, 2018 at 22:28
Add a ment  | 

1 Answer 1

Reset to default 13

This happens because while you had created collections within the document, you never created the document itself. You can tell whether documents exist because if they dont they will show up in Firestore as italics.

发布评论

评论列表(0)

  1. 暂无评论