简体   繁体   中英

Invalid document reference. Document references must have an even number of segments, but likes has 1

What's an issue in my Query?

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

It has a value to addDocument . value is not nil.

I encountered this error for the first time. what is the reason?

There was an error because there was not a value for let itemId = selectedItem.objectID .

Therefore, when I set it directly in if without declaring a constant, the error disappeared.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM