2021年2月10日星期三

how to validate document paths in firestore?

So, in my previous question, I ended up figuring out my own issue, (I would recommend taking a look at that before reading this one), but the 20 seconds of glory was cut short when I realized that the outcome was similar across all users on the app, which is what I didn't want and totally forgot about.

With the function down below, I can purchase the event and the buttons will show up for that event and go away if I cancel, and it's unique for each event, which I adore. Now, the problem with the function down below is that if I make a purchase on user1 account and the buttons show up and stay there how they're supposed to, when I log into user2 account and perhaps want to purchase that same event, the buttons are already showing up even though user2 hasn't done anything.

                getSchoolDocumentID { (schoolDocID) in                  if let schID = schoolDocID {                      self.db.document("school_users/\(schID)/events/\(self.selectedEventID!)").getDocument { (documentSnapshot, error) in                          if let error = error {                              print("There was an error fetching the document: \(error)")                          } else {                                guard let docSnap = documentSnapshot!.get("purchased") else {                                  return                              }                              if docSnap as! Bool == true {                                  self.viewPurchaseButton.isHidden = false                                  self.cancelPurchaseButton.isHidden = false                                  self.creditCard.isHidden = true                                  self.purchaseTicketButton.isHidden = true                                } else {                                  self.creditCard.isHidden = false                                  self.purchaseTicketButton.isHidden = false                              }                          }                      }                  }              }  

So i tried to solve the problem on my own but ran into a roadblock. I tried to make a subcollection of events_bought when users purchase an event and have the details stored in fields that I can call later on in a query. This was something I thought I could use to make the purchases unique amongst all users.

The function below looks through events_bought subcollection and pulls up a field and matches it with a piece of data on the displayedVC, the issue is if the event hasn't been purchased and I go on it with that user, it crashes and says how the document reference path has the wrong number of segments which I don't get because it's the same as the function above, so I realized that the path wouldn't exist and tried to figure out ways to validate the path and came up with the function down below.

getEventsBoughtEventID { (eventBought) in          if let idOfEventBought = eventBought {                            let docPath = self.db.document("student_users/\(self.user?.uid)/events_bought/\(idOfEventBought)")                            if docPath.path.isEmpty {                  self.creditCard.isHidden = false                  self.purchaseTicketButton.isHidden = false              } else {                  self.db.document("student_users/\(self.user?.uid)/events_bought/\(idOfEventBought)").getDocument { (documentSnapshot, error) in                      if let error = error {                          print("There was an error trying to fetch this document: \(error)")                      } else {                          guard let docSnapEventName = documentSnapshot!.get("event_name") else {                              return                          }                                                    if docSnapEventName as! String == self.selectedEventName! {                              self.viewPurchaseButton.isHidden = false                              self.cancelPurchaseButton.isHidden = false                              self.creditCard.isHidden = true                              self.purchaseTicketButton.isHidden = true                          }                      }                  }              }          }      }  

I wasn't really sure if it would work or not so I tried my luck, but I still end up getting the same document reference errors. If anyone can figure out how I can validate a document path and use logic to make certain things happen, that would be great. Thanks.

https://stackoverflow.com/questions/66145378/how-to-validate-document-paths-in-firestore February 11, 2021 at 05:33AM

没有评论:

发表评论