我正在嘗試編寫一些代碼,用於將用戶數據創建/插入我的Firebase應用(基於Web)。未正確創建或插入Firebase數據庫的用戶數據
我想在用戶通過他們的電子郵件&驗證密碼(該部分正常工作)後執行此操作。
但是我在將用戶的ID,用戶名,電子郵件,年齡和個人資料圖片存儲在Firebase數據庫中時遇到了一些麻煩。
這是數據庫結構我的目標:
MyAppReference
|
|___users
|
|_____UID
|___username
|___email
|___age
|___profile_picture
當用戶登錄後,與他們的電子郵件&密碼生成用戶ID(UID),我想將其保存爲用戶的「用戶」字段內的唯一標識符。 然後在他們的UID中,我想存儲他們的用戶名,電子郵件,年齡和profile_picture。 但我不知道如何在同一段代碼中創建父代(UID)和子代。
這是我目前的嘗試:
function insertUserDetails() {
var useremail = "[email protected]";
var userpassword = document.getElementById("passwordfield").innerHTML;
firebase.auth().createUserWithEmailAndPassword(useremail, userpassword).then(function(user) {
console.log("User is authenticated, attempting to enter the user data now...");
// Define variables for sending to database:
var username = "JohnDoe";
var userId = user.uid;
var age = "50";
var profile_picture = "http://zntent.com/wp-content/uploads/2016/03/will-ferrell-film.jpg";
function writeUserData(userId, username, useremail, age, profile_picture) {
// Attempting to create the parent "UID" (child of "users") here:
function addStore(){
var rootRef = firebase.database().ref();
var storesRef = rootRef.child('MyAppReference/users');
var newStoreRef = storesRef.push();
newStoreRef.set({
"UID": userId,
});
}
// Attempting to create/insert user data under that user's UID:
firebase.database().ref('users/' + userId).set({
username: username,
email: email,
age: age,
profile_picture : profile_picture
});
}
}, function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
Console.log("There was an error: " + errorCode + "." + errorMessage);
// ...
});
}
這不是工作,但我沒有收到在控制檯中的任何錯誤,當我進行測試。用戶正在進行身份驗證,他們的用戶名和用戶ID出現在Firebase控制檯中,但數據庫中沒有他們的蹤跡。
這樣做的正確方法是什麼?我習慣了SQL,這是我第一次使用noSQL數據庫,所以也許我的事情完全錯了?
任何幫助解決這個問題將非常感激。先謝謝你!
啊完美!謝謝! – Emily
您好@arnehugo我發佈了一個不同的問題,涉及從Facebook標誌中插入數據到Firebase數據庫中,但沒有得到回覆。如果你有空閒時間,我真的很感激你的想法:http://stackoverflow.com/questions/40591681/how-to-send-facebook-authentication-details-to-firebase-using-a-cordova-plugin再次感謝。 – Emily