![](/img/trans.png)
[英]"FIRESTORE INTERNAL ASSERTION FAILED: Invalid document reference. Document references must have an even number of segments, but NewGame has 1"
[英]Thread 1: FIRESTORE INTERNAL ASSERTION FAILED: Invalid document reference. Document references must have an even number of segments, but Posts has 1?
為什么我在嘗試創建 firebase 計數器時不斷收到此錯誤? 我實際上是在跟蹤 google firebase docs line for line。 這是創建計數器功能
func createCounter(ref: DocumentReference, numShards: Int) {
ref.setData(["numShards": numShards]){ (err) in
for i in 0...numShards {
ref.collection("shards").document(String(i)).setData(["count": 0])
}
}
}
這就是我嘗試使用它的方式
Button("In there"){createCounter(ref: ref.document("Posts"), numShards: 0); incrementCounter(ref: ref.document("Posts"), numShards: 0); getCount(ref: ref.document("Posts"))
}
當我有這個時,我也不斷收到這個“從初始化程序返回而不初始化所有存儲的屬性”錯誤。
struct PostRow: View {
var post: PostModel
@ObservedObject var postData : PostViewModel
let db = Firestore.firestore()
let uid = Auth.auth().currentUser!.uid
let numShards: Int
let count: Int
init(numShards: Int, count: Int) {
self.numShards = numShards
self.count = count
}
在 SwiftUI 視圖的init(){}
中使用此代碼。
init() {
UINavigationBar.appearance().barTintColor = UIColor.clear
UINavigationBar.appearance().tintColor = .clear
UINavigationBar.appearance().isOpaque = true
}
您收到錯誤是因為您沒有在“init(...)”中初始化所有變量。 嘗試這樣的事情:
struct MAPostRow: View {
let loc: String = "Massachusetts" // <--- initialized
var post: PostModel // <-- not initialized
@ObservedObject var MApostData : MAPostViewModel // <-- not initialized
let uid = Auth.auth().currentUser!.uid // <-- avoid doing this "!"
init(post: PostModel, MApostData: MAPostViewModel) { // <-- need to do this
self.post = post // <--- now initialized
self.MApostData = MApostData // <--- now initialized
UINavigationBar.appearance().barTintColor = UIColor.clear
UINavigationBar.appearance().tintColor = .clear
UINavigationBar.appearance().isOpaque = true
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.