【发布时间】: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