简体   繁体   English

如何从Firebase数据库获取离子数据?

[英]How to get data from Firebase database to ionic?

I am having an issue (new to JavaScript and ionic) regarding Firebase database. 我遇到了有关Firebase数据库的问题(JavaScript和Ionic的新问题)。 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. 当我直接使用-KcsNqRpO70spcMIPaKg ,这可以很好地工作,但是我希望我的代码通过简单地将登录用户与其数据库中的帐户进行匹配来直接获取此字符串。

I tried using var userId = firebase.auth().currentUser.uid; 我尝试使用var userId = firebase.auth().currentUser.uid; instead, but it doesn't grab the -KcsN... , instead it grabs the actual uid from the authentication. 而是,它不捕获-KcsN... ,而是从身份验证中捕获实际的uid。

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 : 好了,经过几个小时的搜索,我希望我能像我一样帮助新手,答案是实际使用“ Set”而不是push,并将Firebase创建的随机密钥重命名为用户的UID:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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