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

时间:2017-07-17 07:20:30

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

这是我的注册控制器,我已成功注册,但我不知道如何存储其他字段,如姓名,职业等。我该怎么做?

控制器

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

    };
});

我是否必须在当时的功能中执行某些操作,或者已经有一些事情可以解决这个问题了?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

<强>最后 我自己弄清楚了。

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

};

});