2015-11-27 48 views
1

我有這樣一個數據:檢索子記錄時父鍵是未知的火力地堡

"customers": { 
    "aHh4OTQ2NTlAa2xvYXAuY29t": { 
     "customerId": "xxx", 
     "name": "yyy", 
     "subscription": "zzz" 
    } 
} 

我需要通過客戶ID以檢索客戶。由於路徑限制,父密鑰只是B64編碼的郵件地址。通常我通過這個電子郵件地址查詢數據,但有幾次我只知道客戶ID。我試過這個:

getCustomersRef() 
    .orderByChild('customerId') 
    .equalTo(customerId) 
    .limitToFirst(1) 
    .once('child_added', cb); 

這很適合客戶真正存在的情況。在相反的情況下,回調從未被調用。

我試圖value事件,工作,但給我整棵樹開始編碼的電子郵件地址,所以我不能達到內部的實際數據。或者我可以嗎?

我發現這個答案Test if a data exist in Firebase,但再假定你,我知道所有的路徑元素。

getCustomersRef().once('value', (snapshot) => { 
    snapshot.hasChild(`customerId/${customerId}`); 
}); 

我還能在這裏做什麼?

更新

我想我找到了解決方案,但感覺不對。

let found = null; 
snapshot.forEach((childSnapshot) => { 
    found = childSnapshot.val(); 
}); 
return found; 
+0

可能的複製[如何查詢火力與特殊價值屬性的所有孩子裏面(http://stackoverflow.com/questions/25272919/how-to-query-firebase-for-財產與特定的價值,由內所有子女) –

+1

同樣[這裏](http://stackoverflow.com/a/14965065/1300910)的回答可以幫助你 –

+0

你能澄清這個問題嗎?它聲明你想'當父母關鍵字未知時檢索一個孩子記錄'。這就是firebase查詢所做的 - 通過一組節點查看它們是否有任何子節點與您指定的數據匹配 - 父節點不需要指定,只有父節點是父節點包含您正在查找的孩子的節點。 – Jay

回答

1

old;誤解了這個問題:

如果你知道 「endcodedB64Email」,這是這樣的:

var endcodedB64Email = B64_encoded_mail_address; 

firebase.database().ref(`customers/${endcodedB64Email}`).once("value").then(snapshot => { 
     // this is getting your customerId/uid. Remember to set your rules up for your database for security! Check out tutorials on YouTube/Firebase's channel. 
     var uid = snapshot.val().customerId; 
     console.log(uid) // would return 'xxx' from looking at your database 

// you want to check with '.hasChild()'? If you type in e.g. 'snapshot.hasChild(`customerId`)' then this would return true, because 'customerId' exists in your database if I am not wrong ... 
}); 

UPDATE(修正):我們必須

至少知道一個關鍵。所以,如果你在某些情況下 只知道客戶的uid-鍵,那麼我會做這樣的:

// this is the customer-uid-key that is know. 
var uid = firebase.auth().currentUser.uid; // this fetches the user-id, referring to the current user logged in with the firebase-login-function 
// this is the "B64EmailKey" that we will find if there is a match in the firebase-database 
var B64EmailUserKey = undefined; 

// "take a picture" of alle the values under the key "customers" in the Firebase database-JSON-object 
firebase.database().ref("customers").once("value").then(snapshot => { 
    // this counter-variable is used to know when the last key in the "customers"-object is passed 
    var i = 0; 
    // run a loop on all values under "customers". "B64EmailKey" is a parameter. This parameter stores data; in this case the value for the current "snapshot"-value getting caught 
    snapshot.forEach(B64EmailKey => { 
      // increase the counter by 1 every time a new key is run 
      i++; 
      // this variable defines the value (an object in this case) 
      var B64EmailKey_value = B64EmailKey.val(); 
      // if there is a match for "customerId" under any of the "B64EmailKey"-keys, then we have found the corresponding correct email linked to that uid 
      if (B64EmailKey_value.customerId === uid) { 
       // save the "B64EmailKey"-value/key and quit the function 
       B64EmailUserKey = B64EmailKey_value.customerId; 

       return B64UserKeyAction(B64EmailUserKey) 
      } 

      // if no linked "B64EmailUserKey" was found to the "uid" 
      if (i === Object.keys(snapshot).length) { 
       // the last key (B64EmailKey) under "customers" was returned. e.g. no "B64EmailUserKey" linkage to the "uid" was found 
       return console.log("Could not find an email linked to your account.") 
      } 
    }); 
}); 

// run your corresponding actions here 
function B64UserKeyAction (emailEncrypted) { 
     return console.log(`The email-key for user: ${auth.currentUser.uid} is ${emailEncrypted}`) 
} 

我建議把這個在函數或類,所以你可以輕鬆地調用它並以有組織的方式重用代碼。

我也想要添加,您的firebase的規則必須定義,使一切安全。如果必須計算敏感數據(例如價格),那麼在Firebase的服務器端執行此操作!使用雲功能。這是新換的火力地堡2017年

+0

這有點古老的問題,我不記得我是如何解決它的,但正如我上面所說的「但有幾次我只知道customerId」。你的解決方案是期望endcodedB64Email是已知的,而不是。 – FredyC

+0

你好。對於那個很抱歉。如果您只知道'customerId',我現在已經更新瞭如何獲得正確的'endcodedB64Email'的答案。告訴我這是不是你想要的。 – ravo10

+1

謝謝,但是這必須具有可怕的內存佔用,基本上加載所有客戶(直到找到正確的內存)。我想最好的辦法是,最終將有一個單獨的查找地圖與customeri->電子郵件。 – FredyC