這是任務。當創建用戶時,我想將它們的一個實例添加到主數據庫中,以便他們可以將事物與它們關聯(列表和配方)。我試着做.set()
和.update()
,雖然兩個用戶都在我的數據庫中添加了一個用戶實例,但每次新用戶註冊時都會覆蓋它。我怎麼去關於不覆蓋這些信息?我會根據我找到的信息繼續搜索並更新我的帖子。這是我有的授權碼。創建用戶將該用戶的實例添加到Firebase數據庫?
app.controller("SampleCtrl", ["$scope", "Auth",
function($scope, Auth) {
$scope.auth = Auth;
$scope.createUser = function() {
$scope.message = null;
$scope.error = null;
// Create a new user
Auth.$createUserWithEmailAndPassword($scope.email, $scope.password, $scope.name)
.then(function(firebaseUser) {
$scope.message = "User created with uid: " + firebaseUser.uid;
const uid = firebaseUser.uid;
const dbref = firebase.database().ref().child('users');
dbref.update({
id: uid,
name: $scope.name
});
console.log(dbref.uid);
}).catch(function(error) {
$scope.error = error;
});
};
$scope.deleteUser = function() {
$scope.message = null;
$scope.error = null;
$scope.auth.$onAuthStateChanged(function(firebaseUser) {
$scope.firebaseUser = firebaseUser;
});
// Delete the currently signed-in user
Auth.$deleteUser().then(function() {
$scope.message = "User deleted";
}).catch(function(error) {
$scope.error = error;
});
};
}
]);
在此先感謝!
******編輯**********
好了,所以我要添加信息,而不是在我預料的方式。我想要的結構是這樣的 用戶 -----編號 -------信息 其中id是關鍵,info是保存新對象的值。 我得到的是這個... a random serialized ID and then my user id and information object
現在的問題是,我該如何擺脫隨機序列化的關鍵?
我認爲這是你鏈接的問題的重複訪問。如果我關閉它,你還好嗎(因爲它會增加你的其他/更長的答案的重量)? –
好主意,我忘了標記爲重複 –