更新firebase中的子项无法按预期工作

时间:2017-12-18 18:59:31

标签: swift firebase firebase-realtime-database

我正在尝试将数据保存到firebase中,首先使用.childByAutoId()生成一个子项,然后使用必要的数据更新子项。但它似乎没有按预期工作。

我想要实现的结构是

events
  attendees
   -L0P1D5arR0OkBf8h
      userEmail: "user@user.com"
      userName: "User name"
      userPhone: "0864567182"

这是我到目前为止所做的事情:

    guard let fee = events?["eventFee"] else {
        return
    }
    guard let key = events?["eventKey"] else {
        return
    }
    guard let eventTitle = events?["title"] else {
        return
    }

    if fee == "0" {
        var values = [String: String]()


        self.ref = Database.database().reference()

        let attendeekey = ref.child("events").child(key).child("attendees").childByAutoId().key

        let userDetails = UserDetails()

        for user in userDetails.currentUserDetails {
            guard let userEmail = user.email else {
                return
            }
            guard let firstName = user.firstName, let lastName = user.lastName else {
                return
            }
            guard let userPhone = user.phoneNo else {
                return
            }

            let userName = "\(firstName) \(lastName)"

            values = ["userEmail": userEmail, "userName": userName, "userPhone": userPhone as! String]
        }

        ref.updateChildValues(["events/\(key)/attendees/\(attendeekey)": values], withCompletionBlock: {
            (err, ref) in

            if err != nil {
                self.displayAlertMessage(message: err as! String, title: "Oops!")
                //print(err ?? "An error occured")
                return
            }

            let message = "You have successfully registered for \(eventTitle)"

            self.displayAlertMessage(message: message, title: "Success!")

        })
    }

我的方法有什么问题吗?

0 个答案:

没有答案