2017-06-19 4 views
1

を与えFirebaseデータを取得し、私はFirebaseと私のデータベースを使用しています以下のようになります。私は、たとえば電子メールを取得することができ、スウィフト3を使用して表示名を指定した方法は、特定のフィールドに

Users 
    user1 
     email: "[email protected]" 
     password: "pass1" 
     display name: "Name1" 
    user2 
     email: "[email protected]" 
     password: "pass2" 
     display name: "Name2" 

? (私はName1を知っていれば、例えば、取得されたデータは[email protected]になります。)は、次のヘルパー関数の実装

let dbstrPath : String! = "Users/user1" 
self.dbRef.child(dbstrPath).observeSingleEvent(of: .value, with: { (snapshot) in 
    if snapshot.exists(){ 
     print(snapshot.value!) 
     // Here you got user value in dict 
    } 
}) 
+0

関連:https://stackoverflow.com/questions/43971814/firebase-querying-for-unique- username-swift –

+0

私は表示名を見つけたすべてのデータを取得する方法を理解していますが、電子メールで正確なフィールドに到達する方法はわかりません。私は "snapshot.value"以外に何を書く必要があるのですか? @NiravD – alecs09

+0

@begood私の答えがあなたの後ろのものであるかどうかを確認してください... –

答えて

0

以下のように

1

使用firebase:

func queryEmail(of displayName: String, completion: @escaping (String?) -> Void) { 
    let users = FIRDatabase.database().reference().child("Users") 
    let query = users.queryOrdered(byChild: "display name").queryEqual(toValue: displayName) 
    query.observeSingleEvent(of: .value) { 
     (snapshot: FIRDataSnapshot) in 
     guard snapshot.exists() else { 
      completion(nil) 
      return 
     } 
     let users = snapshot.children.allObjects as! [FIRDataSnapshot] 
     precondition(users.count == 1, "Display name isn't unique!") 
     let userProperties = users.first!.value as! [String: Any] 
     completion(userProperties["email"] as? String) 
    } 
} 

をし、このようにそれを使用します。

queryEmail(of: "Name1") { 
    (email) in 
    if let email = email { 
     print("Name1 email is \(email)") 
    } else { 
     print("Email not found") 
    } 
} 
+0

OPは彼が望むものを得る*クエリー*の後にあると思います。代わりに私の答えを見てください... –

関連する問題