【问题标题】:Passing variable into Firestore document path将变量传递到 Firestore 文档路径
【发布时间】:2019-07-06 02:32:04
【问题描述】:

我正在尝试引用一个变量,我将用户 UID 存储到 Cloud Firestore 的文档引用中。该路径似乎没有读取字符串。我尝试了两种不同的方式,两种方式都给我错误。如何将变量字符串传递到 Cloud Firestore 文档/集合路径?


我正在从数据库中获取 UsernameID:

UsernameID = Auth.auth().currentUser!.uid

var UsernameID = String()

let ref = db.collection("user/\(UsernameID)/account").document("created")
        ref.getDocument { (document, error) in
            if let document = document, document.exists {
                print("User Exists")
            } else {
                print("User does not exist in firestore")
            }
        }

抛出错误:

*** Terminating app due to uncaught exception 'FIRInvalidArgumentException', reason: 'Invalid path (user//account). Paths must not contain // in them.'
libc++abi.dylib: terminating with uncaught exception of type NSException

var UsernameID = String()

let ref = db.collection("user").document(UsernameID).collection("account").document("created")
        ref.getDocument { (document, error) in
            if let document = document, document.exists {
                print("User Exists")
            } else {
                print("User does not exist in firestore")
            }
        }

抛出错误:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'FIRESTORE INTERNAL ASSERTION FAILED: Invalid document reference. Document references must have an even number of segments, but user has 1'

我也尝试将 uid 存储为 DocumentReference,但未能将字符串存储为 DocumentReference。

let UsernameID = DocumentReference!

【问题讨论】:

  • 看起来您的代码正在传递一个空字符串作为文档的 id。那永远行不通。

标签: ios swift firebase google-cloud-firestore


【解决方案1】:

假设您的身份验证正确无误,您可以执行以下操作来访问存储在 Firestore 中的文档。假设结构是

users //collection
   uid //document
      name //field within the document

以及读取名称字段的代码

let uid = Auth.auth().currentUser?.uid
let collectionRef = self.db.collection("users")
let userDoc = collectionRef.document(uid)
userDoc.getDocument(completion: { document, error in
     if let err = error {
         print(err.localizedDescription) //document did not exist
         return
     }

     if let doc = document {
         let name = doc.get("name")
         print(name)
     }
 })

【讨论】:

    【解决方案2】:

    在您的最佳示例中

    db.collection("user/\(UsernameID)/account")
    

    您似乎没有请求您的用户集合。这就是为什么您在集合名称中收到有关 \ 的错误。用户集合应该是这样的

    let ref = db.collection("user").document(UsernameID)
            ref.getDocument { (document, error) in
                if let document = document, document.exists {
                    print("User Exists")
                } else {
                    print("User does not exist in firestore")
                }
            }
    

    .document 应该获取与传递的 UsernameID 对应的用户文档。要获取某个属性,您需要通过文档变量访问它。

    【讨论】:

    • OP 使用的是 Firestore 而不是 Real Time Database
    • 您忽略了 UsernameID 的定义方式,因此如果 OP 使用它,他们将继续收到相同的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 2019-12-04
    相关资源
    最近更新 更多