【问题标题】:creating user in Firebase using angularjs使用 angularjs 在 Firebase 中创建用户
【发布时间】:2019-06-14 03:14:35
【问题描述】:

您好,我正在尝试使用 angularjs 学习 firebase。

索引html

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.5/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular-route.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular-animate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular-route.min.js"></script>

<!-- Firebase Starting -->
<script src="https://www.gstatic.com/firebasejs/5.8.0/firebase.js"></script>
<script>
    // Initialize Firebase
    var config = {
        apiKey: "AIzaSyDbul8FNC4posBAaNKhO8kiz2mdrkD539U",
        authDomain: "angularregistration-b9dec.firebaseapp.com",
        databaseURL: "https://angularregistration-b9dec.firebaseio.com",
        projectId: "angularregistration-b9dec",
        storageBucket: "angularregistration-b9dec.appspot.com",
        messagingSenderId: "1085945127139"
    };
    firebase.initializeApp(config);
</script>

注册控制器

myApp.controller('RegistrationController',
['$scope', '$firebase', '$firebaseAuth',
    function ($scope, $firebase, $firebaseAuth) {

        var ref = firebase.database().ref();
        var auth = $firebaseAuth();

        $scope.login = function () {
            $scope.message = "Welcome " + $scope.user.email;
        };

        $scope.register = function () {

            auth.$createUserWithEmailAndPassword(
                $scope.user.email,
                $scope.user.password
            ).then(function (regUser) {
                $scope.message = "Hi " + $scope.user.firstname + " thanks for registering !";

            }).catch(function (error) {
                $scope.message = error.message;
            }); //createUserWithEmailAndPassword
        }; //register
    }]); //controller

注册HTML

<h3 class="text-center">Register Page</h3>
<form name="myForm" novalidate ng-submit="register()">
    <div class="form-group">
        <label for="First Name">First Name</label>
        <input ng-model="user.firstname" type="text" class="form-control" id="firstname" placeholder="First name">
    </div>
    <div class="form-group">
        <label for="First Name">Last Name</label>
        <input type="text" class="form-control" id="lastname" placeholder="Last name">
    </div>
    <div class="form-group">
        <label for="exampleInputPassword1">Password</label>
        <input type="text" class="form-control" id="exampleInputPassword1" placeholder="Password">
    </div>

    <div class="form-group">
        <label for="exampleInputEmail1">Email address</label>
        <input ng-model="user.email" type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp"
            placeholder="Enter email" ng-required="true">
    </div>
    <button type="submit" class="btn btn-primary" ng-disabled="myForm.$invalid">Register</button>
</form>
<a href="#!login">Back to Login</a>

<p ng-show="message">{{message}}</p>

我正在尝试做的只是尝试将用户注册到 firebase。在我的 index.htm 中,我还先加载 angularjs,然后加载 firebase,然后再加载 angularjsfire。每次我尝试创建用户时,都会出现

错误

{code: "auth/argument-error", message: "createUserWithEmailAndPassword failed: 第二个参数 "password" 必须是有效字符串。"}

我试图在 stackoverflow 中查看另一个答案,但无法找到我试图做错的事情。任何帮助将不胜感激。

【问题讨论】:

  • 如果您在将其传递给createUserWithEmailAndPassword 之前console.log($scope.user.password),它会显示什么?
  • 没什么,这是个问题。我发现好像我正在将空绑定属性传递给firebase,但它不知道密码中的值是什么。非常感谢您的帮助

标签: angularjs firebase firebase-authentication


【解决方案1】:

重新访问我的表单并阅读模板中的所有绑定值后,我忘记在密码字段中绑定值。一旦我绑定了我的密码字段,它就可以正常工作了。

   <div class="form-group">
    <label for="exampleInputPassword1">Password</label>
    <input type="text" ng-model="user.password" class="form-control" id="exampleInputPassword1" placeholder="Password">
</div>

感谢@Frank van Puffelen 的快速建议

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 2018-08-10
    • 1970-01-01
    相关资源
    最近更新 更多