简体   繁体   English

来自firebase的angularjs rootscope用户数据库信息

[英]angularjs rootscope user database info from firebase

I am trying to fetch user info from user's database and access it from anywhere using rootscope. 我试图从用户的数据库中获取用户信息,并使用rootscope从任何地方访问它。
I get the user's email and uid as the user signs in. 当用户登录时,我会收到用户的电子邮件和uid。
However,to get the user's info from database, I need to call my database and read the info one by one and store it as a variable in rootscope to access it from everywhere. 但是,要从数据库中获取用户信息,我需要调用数据库并逐一读取信息,并将其作为变量存储在rootscope中,以便从任何地方访问它。

So, my question is below : 所以,我的问题如下:

  • How can I use rootscope in this example? 在此示例中,如何使用rootscope?
  • Is there any better way to do this? 有什么更好的方法吗?
  • in the below example, the console log is showing the first name, but I don't know how to rootscope it? 在下面的示例中,控制台日志显示了名字,但是我不知道如何对它进行rootscope?

Thanks for help. 感谢帮助。

app.run(['$rootScope', '$state', '$stateParams', '$cookies', "$location",
function ($rootScope, $state, $stateParams, $cookies, $location) {

    firebase.auth().onAuthStateChanged(function(user) {
        if (user) {

            $rootScope.user.uid = user.uid;
            $rootScope.user.email = user.email;

            return firebase.database().ref('/users/' + user.uid).once('value').then(function(snapshot) {

                var firstname = snapshot.val().firstname;
                console.log("first name", firstname);

            });
        } else {
            // No user is signed in.
        }
    });
}]);

There are two approaches to fulfil your needs 有两种方法可以满足您的需求

Approach 1: 方法1:

you can do this $rootScope.authData=user; 您可以这样做$rootScope.authData=user; then access it from anywhere by injecting $rootScope . 然后通过注入$rootScope从任何地方访问它。 but problem in this is that when you will refresh page $rootScope will be empty Check this SO Question 但这个问题是,当你将刷新页面$rootScope将是空选中此SO问题

Approach 2: 方法二:

I will prefer this approach,you can use use $getAuth() function of angularfire , $getAuth is syncronous function which will gives you current user data. 我会更喜欢这种方法,您可以使用使用$getAuth()的函数angularfire$getAuth是syncronous功能,将让你当前的用户数据。

var myUser=$scope.AuthObj.$getAuth();

for more detail check this 有关更多详细信息,请检查此

If you use AngularFire Always use its wrapper methods vs native firebase methods. 如果使用AngularFire,请 始终使用其包装方法与本机Firebase方法。

Do not use 不使用

firebase.auth().onAuthStateChanged
firebase.database().ref('/users/' + user.uid).once('value')

Use 采用

$firebaseAuth().$onAuthStateChanged
var userData = $firebaseObject(firebase.database().ref('/users/' + user.uid));

Checkout the following link for full list of available methods in angularFire . 请查看以下链接以获取angularFire中可用方法的完整列表。 https://github.com/firebase/angularfire/blob/master/docs/reference.md https://github.com/firebase/angularfire/blob/master/docs/reference.md

I recomend modifying your code like this. 我建议像这样修改您的代码。

$firebaseAuth().$onAuthStateChanged(function(user) {
    if (user) {
      $rootScope.user = $firebaseObject(firebase.database().ref('/users/' + user.uid));
      $rootScope.user.uid = user.uid;
      $rootScope.user.email = user.email;
      console.log("Here you should have complete user object");
      // still if you want to do something when user is loaded
      /* $rootScope.user.$loaded(function(loadedUser) {
            var firstname = loadedUser.firstname;
            console.log("first name", firstname);
        });*/
    } else {
        // No user is signed in.
    }
});

Of course you need to include $firebaseObject and $firebaseAuth using dependency injection. 当然,您需要使用依赖项注入包括$firebaseObject$firebaseAuth

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

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