0

I have trouble with getting data from Firebase. I try to fetch Data from firebase. Everything is fine, except for these two lines.

 func calculateDailyAmountOfCalories() {
    let uid = Auth.auth().currentUser?.uid
    let ref = Database.database().reference()
    ref.child("users").child(uid!).observe(.value) { (snapshot) in
        self.userWeight = snapshot.childSnapshot(forPath: "weight").value as!  Float
        self.userHeight = snapshot.childSnapshot(forPath: "height").value as! Float
        self.userAge = snapshot.childSnapshot(forPath: "age").value as! Float
        **var newUserGoal = snapshot.childSnapshot(forPath: "userGoal").value as? Float
        var newUserLevelActivity = snapshot.childSnapshot(forPath: "activityLevel").value as? Float**
        print(newUserGoal)
        print(newUserLevelActivity)



        print(self.userWeight)
        print(self.userHeight)
    }
}

When I try to fetch (var newUserGoal and newUserLevelActivity) data without as?/! Float I can see an answear in my terminel Optional(0.2) for example. When I try to use as? I get nil. why other similar snapshot work, but these not ?

Stormios
  • 127
  • 1
  • 8

0 Answers0