2

这是我的注册控制器,我已经成功注册,但我不知道任何方式来存储一些额外的字段,如名称,职业等。我该怎么做?Angular-Firebase如何在注册期间在有角的Firebase中存储addiotional字段?

控制器

app.controller('signupCtrl', function ($http, $scope, toaster, $location, $firebaseAuth) { 
var ref = firebase.database().ref(); 
auth = $firebaseAuth(firebase.auth()); 

$scope.registerData = {}; 
$scope.register = function() { 

    auth.$createUserWithEmailAndPassword($scope.registerData.email, $scope.registerData.password) 
     .then(function (data) { 
      $scope.message = "Welcome " + data.uid + ", thanks for registering!"; 
      console.log(data.uid); 
     }).catch(function (error) { 
       $scope.message = error.message; 
       console.log($scope.message); 
      }); 

    }; 
}); 

我必须做一些事情,然后在功能还是有一些已经处理呢?

+0

不,您需要将它存储在您的successHandler中,您想要保存数据的位置? –

+0

Firebase身份验证只能保存电子邮件和密码。如果您想保存其他凭据,则必须将其存储在Firebase数据库中。 – UmarZaii

+0

我想将数据保存在数据库中。 @Umarzaii如何? –

回答

1

最后 我想出了自己。

app.controller('signupCtrl', function ($http, $scope, toaster, $location, $firebaseAuth) { 
var ref = firebase.database().ref(); 
auth = $firebaseAuth(firebase.auth()); 
var database = firebase.database(); 
$scope.registerData = {}; 
$scope.register = function() { 
    auth.$createUserWithEmailAndPassword($scope.registerData.email, $scope.registerData.password) 
     .then(function (data) { 
      $scope.message = "Welcome " + data.uid + ", thanks for registering!"; 
      console.log(data.uid); 
      firebase.database().ref('users/' + data.uid).set({username: $scope.registerData.username, role: $scope.registerData.role,}); 
     }).catch(function (error) { 
    $scope.message = error.message; 
    console.log($scope.message); 
}); 

}; 

});

+0

在我的回答中,我向你展示了什么,这不完全一样吗? – UmarZaii

4

如果您希望将您的用户详细信息存储在Firebase中,请改用Firebase数据库。试试这个:

{ 
    "userList": { 
    "JRHTHaIsjNPLXOQivY": { 
     "userName": "userA", 
     "occupation": "programmer" 
    }, 
    "JRHTHaKuTFIhnj02kE": { 
     "userName": "userB", 
     "occupation": "clerk" 
    } 
    } 
} 

您必须创建一个函数,在您成功创建帐户后将数据保存到数据库。您的控制器应该是这样的:

auth.$createUserWithEmailAndPassword($scope.registerData.email, $scope.registerData.password) 
    .then(function (data) { 
     writeUserData(userId, name, useroccupation); 
     $scope.message = "Welcome " + data.uid + ", thanks for registering!"; 
     console.log(data.uid); 
    }).catch(function (error) { 
     $scope.message = error.message; 
     console.log($scope.message); 
    }); 

}; 

function writeUserData(userId, name, useroccupation) { 
    firebase.database().ref('users/' + userId).set({ 
     username: name, 
     occupation: useroccupation 
    }); 
} 

您可以看到函数writeUserData在用户成功创建后调用。

确保您保存了userID中的所有详细信息。希望能帮助到你。 :D

+0

为了做到这一点,我会在控制器中写什么? –

+1

我已经在这篇文章中声明了'writeUserData()'是在用户创建完成后调用的,这样用户的详细信息就可以保存在Firebase数据库中。 – UmarZaii

相关问题