无法向Firebase添加数据

时间:2019-05-15 10:38:21

标签: swift firebase firebase-realtime-database

尝试将数据添加到Firebase,但是数据没有添加到Firebase上,这是我的代码?

@IBAction func addButton(_ sender: UIButton) {
    if let text = self.addWeight.text{
        if let date = self.addDate.text{
            self.saveTaskWithName(name: text, date: date, completionHandler: { (success) in
                // data save successfully
                print("**** TASK ADDED *******")
            })
        }
    }
}

func saveTaskWithName(name:String,date:String, completionHandler:@escaping ((_ success:Bool)->Void)){
        let dict = ["weight": name, "date" : date] as [String : Any]
        if let userid = Auth.auth().currentUser?.uid{
            self.ref.child("WeightTracker/\(userid)").childByAutoId().setValue(dict) { (error, reference) in

                if let err = error{
                    print(err.localizedDescription)
                }else{
                    // data save successfully
                    completionHandler(true)
                }
            }

        }
    }

1 个答案:

答案 0 :(得分:1)

请尝试以下解决方案

func saveTaskWithName(name:String,date:String, completionHandler:@escaping ((_ success:Bool)->Void)){
        let dataBaseRef = Database.database().reference()

        let dict = ["weight": name, "date" : date] as [String : Any]
        if let userid = Auth.auth().currentUser?.uid{
            dataBaseRef.child("WeightTracker").child(userid).setValue(dict) { (error, reference) in
                if let err = error{
                    print(err.localizedDescription)
                }else{
                    // data save successfully
                    completionHandler(true)
                }
            }

        }
    }