简体   繁体   中英

How to get data from Firebase database to ionic?

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?

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);
          });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM