Ios 如果存在于swift中,如何从firestore中删除文档?
我正在尝试验证用户是否已经 以前有没有喜欢过商店。如果他没有这样做,则会将数据添加到firestore集合中,否则,如果他再次单击,则会将其删除 现在,每次我单击按钮,它就会添加一个新文档,如果他再次单击,我就不知道如何删除现有文档Ios 如果存在于swift中,如何从firestore中删除文档?,ios,swift,google-cloud-firestore,Ios,Swift,Google Cloud Firestore,我正在尝试验证用户是否已经 以前有没有喜欢过商店。如果他没有这样做,则会将数据添加到firestore集合中,否则,如果他再次单击,则会将其删除 现在,每次我单击按钮,它就会添加一个新文档,如果他再次单击,我就不知道如何删除现有文档 @IBAction func tapFaveBtn(_ sender: Any) { let name = shopName.text! let address = detailedAdress.t
@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()
}
}
}
}
}
}
有什么帮助吗?谢谢您正在向
Firebase
发送嵌套请求(第一个:获取文档
,第二个:添加文档
或删除
)。将代码更改为:
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)")
}
}
}
}
您正在使用
whereField(“userID”,isEqualTo:userID)
查询数据库,但在添加新文档时,您指定了问题所在的usersID
。将任一字段键更改为查询correctly@Ryohei非常感谢,这是我的领域中的一个输入错误。谢谢你指出它!!!谢谢您的回答,但是每次我单击按钮时,它仍然会添加一个新文档,如果该文档存在,我如何删除它。例如,用户在添加文档后单击,然后再次单击,文档将被删除。请尝试在“self.isExist=true”下添加“break”,谢谢您的回答,但是,如果它存在,我将如何删除现有文档?同时,您正在用户集合中搜索。如果存在店铺id和用户id,我需要在收藏夹集合中搜索,如果存在,我将删除此文档,否则我将使用document.reference.delete()添加新文档方法如果要删除现有文档,您可以将其删除,但我建议您更新现有文档,而不是删除:)您可以根据代码更改表名。将我的项目中的工作代码提供给您:)问题是,上面的代码接受文档参数。这不是我想做的。你的方式对我不起作用。不过,谢谢你
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
}
}
}