2016-09-24 106 views
0

我在我的火力點數據庫如下:檢測如果電子郵件中已經存在火力

Users 
-VCVVsqwws_QSds2ADC1 
    email: 
     "[email protected]" 
-VCVxCaQ_dSwWBC21AB4 
    email: 
     "[email protected]" 

,然後在我的.js文件中的行:

firebase.initializeApp(config); 
var register = firebase.database().ref("Users"); 

如何我已經檢查,如果用戶包含之前的電子郵件它到Users?我試圖遵循https://gist.github.com/anantn/4323949,但瀏覽器表示無法實例化Firebase(新的Firebase),即使所有內容都已導入幷包含在內。

回答

0

您可以使用下面的代碼:

var register = firebase.database().ref("Users/{{userId}}"); 

register.once('value', function(snapshot) { 
    if(snapshot.val().hasOwnProperty('email')) { 
    console.log("Email exist."); 
    } 
    else { 
    console.log("Email not exist."); 
    /* Code to push email to the specific user */ 
    } 
}); 
+0

'userId'從哪裏來? @ ti2005 –

+0

在你的例子中,'VCVVsqwws_QSds2ADC1'是一個userid權限? –

+0

@TomRussell - 我想知道的是,'VCVVsqwws_QSds2ADC1'是用戶的ID還是隻是一個動態密鑰? –

0

您也可以使用下面的代碼(觸發報警:ES6)如果你需要檢查只有一個用戶的真快:

firebase.database().ref(`Users/${userId}`)once('value').then(snapshot => { 
    if(snapshot.val().hasOwnProperty('email')) { 
    console.log("Email exists."); 
    } 
    else { 
    console.log("Email doesn't exist."); 
    /* Code to push email to the specific user */ 
    } 
}); 

請注意,ti2005's snippet工作得很好,但它會加載所有用戶,並且該過程將隨着數據庫的增長而變慢。

+0

'userId'從哪裏來? @GabrielAntunes –

+0

這是一個通常包含UID Firebase決定給用戶的字符串。他們看起來像這樣:_434Pv8IVynZRkV7VX5kFahzDheD5_。您可以通過以下網址檢查數據庫UID:https://console.firebase.google.com/project/YOUR_PROJECT/authentication/users –

相關問題