2016-06-02 3 views
3

ユーザーIDに基づいてユーザーを検索するクエリがあります。Firebase snapshot.keyは実際のキーを返していないのですか?

usersRef.queryOrderedByChild("email").queryEqualToValue(email).observeEventType(.Value, withBlock: { snapshot in 
    if snapshot.exists() { 
     print("user exists") 
     print(snapshot.key) 

クエリが正しいユーザを返すが、ラインprint(snapshot.key)は文字通り単語「ユーザー」、およびない実際のユーザIDを返します。 print(snapshot)戻り、次のユーザー:

Snap (users) { 
    DELyncz9ZmTtBIKfbNYXtbhUADD2 =  { 
     email = "[email protected]"; 
     "first_name" = test; 
     "last_name" = test; 
    }; 

どのように私はDELyncz9ZmTtBIKfbNYXtbhUADD2を得ることができますか? let email = child.value["email"]を使用してメールを受け取ることはできますが、名前付き属性ではないため、鍵を取得できません。

ありがとうございます!

編集:フランクの答えのおかげでコードが更新されました。あなたが場所でクエリを実行するとambiguous use of key

query.observeEventType(.Value, withBlock: { snapshot in 
      print(snapshot.key) 

      if snapshot.exists() { 
       print("user exists") 

       for child in snapshot.children { 
        print(child.key) 

答えて

4

取得、結果が一致した子のリストになります。一致する項目が1つしかない場合でも、結果は1つの子のリストになります。

結果の子どものすべてのキーを印刷しています。結果は1つではないので、問い合わせた位置/コレクションのキーはusersです。

あなたはおそらく探していると、マッチングの子供をループにし、そのキーを印刷:

let query = usersRef.queryOrderedByChild("email").queryEqualToValue(email) 
query.observeEventType(.Value, withBlock: { snapshot in 
    for child in snapshot.children { 
     print(child.key) 
    } 
}) 
+0

はそれを考え出しました! '単にkey = child.keyをStringとしましょう '。再度、感謝します!! – winston

関連する問題