【问题标题】:"FIRESTORE INTERNAL ASSERTION FAILED: Invalid document reference. Document references must have an even number of segments, but NewGame has 1"“FIRESTORE INTERNAL ASSERTION FAILED: Invalid document reference. Document references must have a even number of segments, but NewGame has 1”
【发布时间】:2020-08-18 19:01:53
【问题描述】:

我正在编写快速编程,但每次我运行我的应用程序时,它都会突然崩溃。 我得到的错误信息是

线程 1:异常:“FIRESTORE INTERNAL ASSERTION FAILED: Invalid document reference。文档引用必须有偶数个段,但 NewGame 有 1 个”

我搜索了错误,人们说如果我在 db 中有多个集合和文档,它往往会发生。但是我的数据库真的很简单。只有两个集合,其中我有文件。喜欢这张图。

当我点击一个条形按钮时,应用程序崩溃了,就是这行代码。

@IBAction func addPressed(_ sender: UIBarButtonItem) {
    
    performSegue(withIdentifier: K.homeToGameScreen, sender: self)
    
    // create new game array in db & player's ready status -> true
    
    db.collection(K.FStore.newGameCollection)
        .addDocument(data: [K.FStore.gameBoardField: GameBoard.gameBoard, K.FStore.player1Field: playerInfo[K.FStore.nameField]!, K.FStore.player2Field: K.FStore.player2Field, K.FStore.uID: playerInfo[K.FStore.uID]!]) { (err) in
            
            if let err = err {
                print("Error getting documents3: \(err)")
            } else {
                
                self.db.collection(K.FStore.playersCollection).document(self.docId).updateData([K.FStore.isReadyField: true]){ err in
                    if let err = err {
                        print("Error updating player's isReady status: \(err)")
                    } else {
                        print("Document successfully updated")
                    }
                }
            }
    }
}

上面的代码在一个叫做 HomeViewController 的地方,但我总是在 AppDelegate.swift 文件中出错。

谁能告诉我这里发生了什么以及如何解决这个问题?

【问题讨论】:

  • 你最终找到出路了吗?

标签: swift firebase google-cloud-firestore


【解决方案1】:

几乎可以肯定,self.docId 在使用时是 nil 或空字符串,这会阻止它为构建 DocumentReference 的代码创建路径段。我们看不到你是如何分配它的,甚至看不到它是在哪里定义的,所以你必须调试它并弄清楚为什么它没有你期望的值。

【讨论】:

  • 我遇到了类似的情况,我需要在新用户注册后立即存储用户信息。那么确保doc ID不为空或nil的常见解决方案是什么?
猜你喜欢
  • 2020-09-03
  • 2017-08-14
  • 2021-12-26
  • 2022-01-16
  • 2013-02-16
  • 2021-06-28
  • 2016-11-13
  • 2021-06-26
  • 2011-09-25
相关资源
最近更新 更多