簡體   English   中英

如何快速從Firebase中為用戶獲取特定數據?

[英]how can i get a specific data for a user from firebase in swift?

我試圖創建一個用戶信息數據庫,並通過使用Firebase為每個單個用戶觀察它們。 我認為創建部分運行良好,但是當我調用observeSingleEvent並打印快照值時,它表示快照為空。 你們能幫我嗎?

這是我的Firebase數據庫控制台顯示的內容;

>sinav-be280
>>member
>>>'SffbOC1UoYcmfBNCrjn2aOhFM4F2'
>>>>mail: "1@1.com"

>>>>name: "1"

>>>>password: "123123"

>>>>phone: "1"

這是我的代碼

func createUserIfno() {
  let userInfoDB = ref.child("member")

  let userDictionary = [
    "name": self.nameTextView.text!,
    "phone": self.phoneNumberTextField.text!,
    "mail": Auth.auth().currentUser ? .email!,
    "password" : self.passwordTextField.text!,
  ]
  userInfoDB.child(userID!).setValue(userDictionary) {
    (error, reference) in

    if error != nil {
      print(error!)
    } else {
      print("info saved")
    }
  }
}

func printUserInfo() {
  let userInfoDB = ref.child("member").child(userID!)
  userInfoDB.keepSynced(true)
  userInfoDB.observeSingleEvent( of: .value, with: {
    (snapshot) in
    if !snapshot.exists() {

      print("snapshot does exist")
      print(snapshot)
      return
    }
  })
}

它說 ;

snapshot does exist
Snap (1jMpyCCbt0crfUdINnlSupRdcqk1) <null>

而且我什至不知道1jMpyCCbt0crfUdINnlSupRdcqk1 ID是什么。

其實我玩了一點代碼並解決了問題。

在我的應用程序中,我編寫了用於分別創建用戶和創建數據庫的代碼,因此在firebase中,第一個用戶ID以某種方式進入第二個用戶的信息列表,第二個進入第三個用戶信息列表。 所以我在createUser函數中調用了我的createUserInfo函數,並解決了問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM