Trying to read the email of the current user(user1) who registered as a "Guests" in a certain hotel. so in my tableView, there will be a list of hotels that the current user entered as a guest.
- Hotels
- (AutoID)
- Name: Mariot
- Adress: ########
- Contact: ########
- Email: ########
- uniqueID: ########
- Guests
- (AutoID)
- Email: user1@gmail.com
- Contact: ########
- (AutoID)
- Name: Discovery
- Adress: ########
- Contact: ########
- Email: ########
- uniqueID: ########
- Guests
- (AutoID)
- Email: user1@gmail.com
- Contact: ########
I tried this code but it didnt work.
var hotelRequests : [DataSnapshot] = []
override func viewDidLoad() {
super.viewDidLoad()
if let email = Auth.auth().currentUser?.email {
Database.database().reference().child("Hotels").queryOrdered(byChild: "Name").queryEqual(toValue: email).observe(.childAdded, with: { (snapshot) in
if let hotelDictionary = snapshot.value as? [String:AnyObject] {
if let nameOfHotel = hotelDictionary["name"] as? String {
} else {
self.hotelRequests.append(snapshot)
self.tableView.reloadData()
}
}
}
}
}
I want to see a list in my tableView of hotels that the current user(user1) became a guest.
UPDATED CODE
var hotels : [DataSnapshot] = []
var guest : [DataSnapshot] = []
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as UITableViewCell
if (tableView.tag == 1) {
if let email = Auth.auth().currentUser?.email {
Database.database().reference().child("hotels").queryOrdered(byChild: "email").queryEqual(toValue: email).observeSingleEvent(of: .childAdded, with: { (snapshot) in
let snapshot = self.hotels[indexPath.row]
if let hotelsDictionary = snapshot.value as? [String:AnyObject] {
if let typeOfhotel = hotelsDictionary["typeOfHotel"] as? String {
cell.textLabel?.text = typeOfhotel
}
}
})
}
}
else if (tableView.tag == 2) {
if let email = Auth.auth().currentUser?.email {
Database.database().reference().child("guest").queryOrdered(byChild: "guest Email").queryEqual(toValue: email).observeSingleEvent(of: .childAdded, with: { (snapshot) in
let snapshot = self.guest[indexPath.row]
if let guestDictionary = snapshot.value as? [String:AnyObject] {
if let aTypeOfguest = guestDictionary["TypeOfGuest"] as? String {
cell.textLabel?.text = aTypeOfguest
}
}
})
}
}
return cell
}