【发布时间】:2020-09-15 07:52:31
【问题描述】:
我正在尝试验证用户是否已经 以前喜欢或不喜欢一家商店。如果他不这样做,它会将数据添加到 Firestore 集合中,否则如果他再次单击它会删除它。
现在每次我点击它添加一个新文档的按钮时,如果他再次点击,我无法弄清楚如何删除现有文档。
@IBAction func tapFaveBtn(_ sender: Any) {
let name = shopName.text!
let address = detailedAdress.text!
let table = numTable.text!
let summaryS = summary.text!
let timingS = timing.text!
let userID = Auth.auth().currentUser!.uid
db.collection("Favorites").whereField("shopPID", isEqualTo: getKey!).
whereField("userID", isEqualTo: userID).
getDocuments(){
querySnapshot, error in
if let error = error {
print(error.localizedDescription)
}else
{
if((querySnapshot!.isEmpty)){
self.db.collection("Favorites").addDocument(data:[
"ShopHeaderImg": self.headerImgURL!,
"ShopProfileImg":"",
"address": address,
"costEst": self.costEst.text!,
"country": "",
"latitude": self.getFinalLatitude!,
"location": self.location!,
"longitude": self.getFinalLongitude!,
"name": name,
"numTables": table,
"shopPID": self.getKey!,
"summary": summaryS,
"timing": timingS,
"usersID": userID,
])
print("saved")
}else {
for document in querySnapshot!.documents{
document.reference.delete()
}
}
}
}
}
}
对此有什么帮助吗?谢谢
【问题讨论】:
-
您正在使用
whereField("userID", isEqualTo: userID)查询您的数据库,但是当您添加一个新文档时,您指定usersID这就是问题所在。更改任一字段键以正确查询 -
@Ryohei 非常感谢,这是我 whereField 中的错字。谢谢指出!!!
标签: ios swift google-cloud-firestore