如何访问firebase数据库中的用户UID

时间:2017-05-14 19:44:46

标签: javascript angularjs firebase firebase-realtime-database firebase-authentication

我正在开发一个基于本地联赛的幻想足球网络应用程序。到目前为止,当使用firebase auth创建用户时,我的firebase中使用用户uid创建了一个相应的节点,如下所示:

  $scope.signUp = function (){
   var username = $scope.user.email;
   var password = $scope.user.password;
   var teamname = $scope.user.teamname;
   var fullname = $scope.user.fullname;

   var ref = firebase.database().ref().child('users');

   if(username && password){
     var auth = $firebaseAuth();
         auth.$createUserWithEmailAndPassword(username, 
       password).then(function(user){
             console.log("User Successfully Created");
            let usersRef = firebase.database().ref("users");
             //child is created under users node with a user's user id as 
        child name
            usersRef.child(user.uid).set({
                email: user.email,
                userName: username,
                teamname: teamname,
                fullname: fullname,
                total: 0,
                week:0
              });

             $location.path('/home');
         }).catch(function(error){
            $scope.errMsg = true;
            $scope.errorMessage = error.message;
         });
          }
          };               

那部分工作正常。在数据库中,JSON树看起来像这样:

users:{
         user UID: {
                    email:
                    teamname:
                    fullname:
                    week:
                  }
       }

我有一个服务用于我的所有观点:

.service('CommonProp',['$location','$firebaseAuth', function($location,$firebaseAuth){
    var user = "";
    var auth = $firebaseAuth();

  return {
      getUser:function(){
          if(user == ""){
         user = localStorage.getItem("userEmail"); 
          }
          return user;
      },
      setUser: function(value){
        localStorage.setItem("userEmail", value);  
         user = value; 
      },
      logoutUser: function(){
         auth.$signOut();
         console.log("Logged Out Successfully");
         user = "";
         localStorage.removeItem("userEmail");
         $location.path('/home');
      }
      };
      }]);

问题

我的问题是,当我进入“选择播放器”视图时,我不知道如何访问用户UID,以便我可以为每周设置每个用户的选择。这是我尝试做的:`< / p>

$scope.saveTeam = function(user){
   $scope.history = [];
   var uid = user.uid;
   var ref2 = firebase.database().ref("users/" + auth.uid + "/week"); 
   ref2.set($scope.history);
};

有没有办法可以在各自的用户uid下访问每个用户各自的“周”子节点?

0 个答案:

没有答案