简体   繁体   English

Angular-Firebase如何在角形火层的注册过程中存储附加场?

[英]Angular-Firebase How to store addiotional fields during signup in angular firebase?

This is my controller for signup, I have successfully signup but I don't know any way to store some additional fields like name, occupation, etc. How can I do that? 这是我的注册控制器,我已成功注册,但我不知道如何存储一些额外的字段,如姓名,职业等。我该怎么做?

controller 调节器

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

    };
});

Do I have to do something in then function or is there already something to handle this ? 我是否必须在当时的功能中执行某些操作或者是否已经有了处理此功能的内容?

If you are looking to store your user details in Firebase, use Firebase database instead. 如果您要在Firebase中存储用户详细信息,请改用Firebase数据库。 Try this: 试试这个:

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

You have to create a function that saves your data to the database after you successfully created the account. 您必须创建一个功能,在成功创建帐户后将数据保存到数据库。 Your controller should be like this: 你的控制器应该是这样的:

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

You can see that the funtion writeUserData is called after the user is successfully created. 您可以看到在成功创建用户后调用函数writeUserData。

Make sure that you save all the details inside userID. 确保将所有详细信息保存在userID中。 Hope it helps. 希望能帮助到你。 :D :d

FINALLY i figure it out myself. 最后我自己弄清楚了。

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

};

}); });

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

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