Reputation: 337
I am having an issue (new to JavaScript and ionic) regarding Firebase database. I have a code to display the name of a user :
.controller('accountController',['$scope', '$firebaseArray', 'CONFIG', '$document', '$state', function($scope, $firebaseArray, CONFIG, $document, $state) {
var userId = '-KcsNqRpO70spcMIPaKg';
return firebase.database().ref('/accounts/' + userId).once('value').then(function(snapshot) {
var displayName = snapshot.val().name;
$scope.$apply(function() {
$scope.displayName = displayName;
});
console.log(displayName);
// ...
});
}])
This works fine when I use directly the -KcsNqRpO70spcMIPaKg
, but I would like my code to get directly this string by simply matching the logged in user to its account in the database.
I tried using var userId = firebase.auth().currentUser.uid;
instead, but it doesn't grab the -KcsN...
, instead it grabs the actual uid from the authentication.
I am lost. I do not understand how to grab it. Any ideas?
Upvotes: 0
Views: 1063
Reputation: 337
Alright, after searching for hours, I hope I will help a newbie just like me for this matter, the answer is to actually use "Set" instead of push and to rename your random key created by Firebase to the UID of the user :
firebase.database().ref().child('/accounts/' + user.uid).set({
name: userSignup.displayname,
email: userSignup.cusername,
password: userSignup.cpassword,
description: "No description for this user",
facebook: "",
twitter: "",
}).then(function() {
// Update successful.
$state.go("login");
}, function(error) {
// An error happened.
console.log(error);
});
Upvotes: 0