【问题标题】:Firebase - doc exists but doc.exists returns falseFirebase - doc 存在,但 doc.exists 返回 false
【发布时间】:2018-01-02 22:15:28
【问题描述】:

我正在尝试测试是否存在带有标题 uid 的文档。我已确认该文档存在于用户集合中的 Firestore 中,但 doc.exists 始终返回 false。请注意,我的文档 uid 仅包含集合(无字段)。

  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.
            // ...
          }

【问题讨论】:

  • 你的console.log(doc) 打印什么吗?
  • 是的,它会打印一个 documentSnapshot 对象,其中 exists 属性设置为 false

标签: javascript firebase google-cloud-firestore


【解决方案1】:

这是因为当您在文档中创建集合时,您从未创建文档本身。您可以判断文档是否存在,因为如果不存在,它们将在 Firestore 中显示为斜体。

【讨论】:

  • 感谢上帝的回答,我已经被困了好几个小时了
  • 我该怎么做?我希望我的文档 id 是用户的 uid 来组织所有数据。我怎样才能做到这一点?
猜你喜欢
  • 1970-01-01
  • 2014-02-27
  • 2017-08-18
  • 1970-01-01
  • 2015-08-17
  • 1970-01-01
  • 2011-10-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多