简体   繁体   English

主题 1:FIRESTORE 内部断言失败:文档参考无效。 文档引用必须有偶数个段,但 Posts 有 1 个?

[英]Thread 1: FIRESTORE INTERNAL ASSERTION FAILED: Invalid document reference. Document references must have an even number of segments, but Posts has 1?

Why do I keep getting this error when trying to create a firebase counter?为什么我在尝试创建 firebase 计数器时不断收到此错误? I am literally following the google firebase docs line for line.我实际上是在跟踪 google firebase docs line for line。 this is the create counter function这是创建计数器功能

  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])
                   }
               }
           }

and this is how I am trying to use it这就是我尝试使用它的方式

 Button("In there"){createCounter(ref: ref.document("Posts"), numShards: 0); incrementCounter(ref: ref.document("Posts"), numShards: 0); getCount(ref: ref.document("Posts"))
                        
                    }

I also keep getting this "Return from initializer without initializing all stored properties" error when I have this.当我有这个时,我也不断收到这个“从初始化程序返回而不初始化所有存储的属性”错误。

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
        
    }

Use this code in the init(){} of SwiftUI View.在 SwiftUI 视图的init(){}中使用此代码。

init() {
      
        UINavigationBar.appearance().barTintColor = UIColor.clear        
        UINavigationBar.appearance().tintColor = .clear
        UINavigationBar.appearance().isOpaque = true

       }

you are getting the error because you are not initialising all your variables in "init(...)".您收到错误是因为您没有在“init(...)”中初始化所有变量。 Try something like this:尝试这样的事情:

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.

相关问题 “FIRESTORE 内部断言失败:文档引用无效。文档引用必须有偶数个段,但 NewGame 有 1 个” - "FIRESTORE INTERNAL ASSERTION FAILED: Invalid document reference. Document references must have an even number of segments, but NewGame has 1" 无效的文档参考。 文档引用必须有偶数个段,但喜欢有 1 个 - Invalid document reference. Document references must have an even number of segments, but likes has 1 无效的文档参考。 文档引用必须有偶数个段,但用户有 1&#39; - Invalid document reference. Document references must have an even number of segments, but Users has 1' 我得到无效的文档参考。 文档引用必须在 xcode 中有偶数段错误 - Im getting Invalid document reference. Document references must have an even number of segments error in xcode '无效的文档参考。 尝试编辑/删除新条目时,文档引用必须具有偶数段 - 'Invalid document reference. Document references must have an even number of segments' when trying to edit/delete new entry Swift - Firestore 文档引用必须具有偶数个段,但 - Swift - Firestore Document references must have an even number of segments, but NSInternalInconsistencyException',原因:'FIRESTORE 内部断言失败 - NSInternalInconsistencyException', reason: 'FIRESTORE INTERNAL ASSERTION FAILED 无效的文档参考 - Invalid document reference 读取包含引用数组的 Firestore 文档 - Reading Firestore Document containing an array of references Swift Firestore 中带有文档参考的嵌套结构 - Nested Struct with Document Reference in Swift Firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM