[英]How to delete a document from firestore if it exists in swift?
i am trying to validate if a user has already liked a store before or not.我正在尝试验证用户之前是否已经喜欢过商店。 if he did not it would add the data to the firestore collection, otherwise if he clicks again it would delete it.
如果他不这样做,它会将数据添加到 firestore 集合中,否则,如果他再次单击它会删除它。
right now everytime i click the button it adds a new document, i am not able to figure out how to delete the existing document if he clicks again.现在每次我单击按钮时都会添加一个新文档,如果他再次单击,我无法弄清楚如何删除现有文档。
@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()
}
}
}
}
}
}
any help on this?有什么帮助吗? thank you
谢谢你
You are sending nested requests to Firebase
(First: getDocuments
, Second: addDocument
or delete
).您正在向
Firebase
发送嵌套请求(第一个: getDocuments
,第二个: addDocument
或delete
)。 Change your code to :将您的代码更改为:
var isExist: Bool = false {
didSet{
setDocument()
}
}
@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.isExist = true
}
}
}
}
func setDocument(){
if self.isExist{
self.db.collection("Favorites").addDocument(data:[
"ShopHeaderImg": self.headerImgURL!,
"ShopProfileImg":"",
"address": address,
"costEst": self.costEst.text!,
"country": "Bahrain",
"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 {
self.db.collection("Favorites").document().delete() { err in
if let err = err {
print("Error removing document: \(err)")
}
}
}
}
first, check if there is an existing document available if no then only inser
func getExistingDocument() {
//Get specific document from current user
let docRef = Firestore.firestore().collection("users").document(Auth.auth().currentUser?.uid ?? "")
// Get data
docRef.getDocument { (document, error) in
if let document = document, document.exists {
let dataDescription = document.data()
print(dataDescription?["firstname"])
} else {
print("Document does not exist")
//here you can perorm insert new document
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.