2017-04-18 42 views
0

我正在製作Firebase練習應用程序,並且遇到此問題。在從Firebase數據庫捕獲值時,我得到了NSNull異常。這裏是代碼Firebase NSNull異常同時拍攝數據快照

user = FIRAuth.auth()?.currentUser 
    ref = FIRDatabase.database().reference() 
    let userid = user.uid 
    if userid != nil 
    { 
     print("User Nid \(userid)") 
     ref.child("users").child(userid).observe(.value, with: { 
      (snapshot) in 
      if(snapshot.exists()){ 
       var user_details = snapshot.childSnapshot(forPath: "\(userid)") 
       var user_det = user_details.value as! Dictionary<String, String> 
       print("User Name \(user_det["name"])") 
      } 
      else{ 
       print("Does not exist") 
      } 
     }) 

    } 

這裏是數據庫。

  • UID

    • 用戶細節

      • 名稱: 「薩爾曼」

      • propic: 「picurl」

+0

這行你得到了'NSNull exception' –

+0

VAR user_details = snapshot.childSnapshot(forPath: 「\(用戶ID)」) –

+0

此線@NazmulHasan –

回答

2

你能嘗試用這種方式

ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in 
    // Get user value 
    let value = snapshot.value as? NSDictionary 
    if let userDetail= value?["user-details"] as? [String:Any] { 
     // let username = value?["name"] as? String ?? "" 
      let username = userDetail["name"] as? String ?? "" 
    } 

    }) { (error) in 
    print(error.localizedDescription) 
} 
+0

這對我工作感謝。 –