无效的文档参考。文档引用的段数必须为偶数,但喜欢的段数为1

时间:2018-12-25 14:42:40

标签: ios swift firebase google-cloud-firestore

查询中有什么问题?

let uid = Auth.auth().currentUser?.uid
let itemId = selectedItem.objectID
var isOn = false

func like(){
if isOn == true {
        likeRef.delete(){err in
            if let err = err{
                print("Error")
            }else{
                self.likeImageView.image = UIImage(named: "detail_like.png")
                self.isOn = false
                print("delete Success")
            }
        }
    }else if isOn == false {
        let like = Like(uid: uid!, itemId: itemId!, status: true)
        db.collection("likes").addDocument(data: like.dictionary()){ err in
            if let err = err {
                print("Error adding document: \(err)")
            } else {
                self.isOn = true
                self.likeImageView.image = UIImage(named: "like_after.png")
            }
        }
        print(isOn)
    }
    }

它的值为addDocument。 值不为零。

我第一次遇到此错误。 是什么原因?

1 个答案:

答案 0 :(得分:0)

发生错误,因为let itemId = selectedItem.objectID没有值。

因此,当我直接设置它而不声明常量时,错误消失了。

if isOn == true {
    db.collection("likes").document(likeObjectID).delete(){err in
        if let err = err{
            print("Error")
        }else{
            self.likeImageView.image = UIImage(named: "detail_like.png")
            self.isOn = false
            print("delete Success")
        }
    }
}else if isOn == false {
    let like = Like(uid: uid!, itemId: itemId!, status: true)
    db.collection("likes").addDocument(data: like.dictionary()){ err in
        if let err = err {
            print("Error adding document: \(err)")
        } else {
            self.isOn = true
            self.likeImageView.image = UIImage(named: "like_after.png")
        }
    }
    print(isOn)
}