【问题标题】:Thread 1: FIRESTORE INTERNAL ASSERTION FAILED: Invalid document reference. Document references must have an even number of segments, but Posts has 1?线程 1:FIRESTORE 内部断言失败:文档引用无效。文档引用必须有偶数个段,但 Posts 有 1 个?
【发布时间】:2021-09-09 02:52:53
【问题描述】:

为什么我在尝试创建 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
        
    }

【问题讨论】:

标签: swift xcode firebase google-cloud-firestore swiftui


【解决方案1】:

在 SwiftUI 视图的init(){} 中使用此代码。

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

       }

【讨论】:

  • 我想向您展示我的代码,但我不知道如何以一种可以让我粘贴的方式回复您的答案。每当我粘贴您刚刚发布的内容“从初始化程序返回而不初始化所有存储的属性”时,我都会收到此错误
  • 我更新了我的问题以向您展示问题。
  • 您需要在 init(){} 中初始化所有变量。请参阅@workingdog 答案
【解决方案2】:

您收到错误是因为您没有在“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   
    }

【讨论】:

  • 当我点击切换到视图控制器时它不起作用,导航栏只是黑色并带走了后退按钮。
猜你喜欢
  • 2020-09-30
  • 1970-01-01
  • 1970-01-01
  • 2019-05-24
  • 2021-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-26
相关资源
最近更新 更多