Firebase未定义

时间:2016-07-22 02:42:39

标签: javascript angularjs firebase firebase-authentication

我正在尝试关于Angular和Firebase的Lynda.com教程,但我遇到了麻烦。我在这个错误上找到了几个页面,但仍然无法解决这个问题。正如在另一个页面上指出的那样,我尝试将firebase.database()。ref()用于'new Firebase()',但它会产生不同的错误。

请帮忙。 TIA!

的index.html:

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <title>Angular Registration</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">

    <script type="text/javascript" src="js/angular.min.js"></script>
    <script type="text/javascript" src="js/angular-route.min.js"></script>
    <script type="text/javascript" src="js/angular-animate.min.js"></script>

    <script src="https://www.gstatic.com/firebasejs/3.1.0/firebase.js"></script>
    <script src="https://www.gstatic.com/firebasejs/3.1.0/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/3.1.0/firebase-auth.js"></script>
    <script src="https://www.gstatic.com/firebasejs/3.1.0/firebase-database.js"></script>

    <script>
      // Initialize Firebase
      var config = {
        apiKey: "AIzaSyCujdl_kIpR42CEqfgC2HxXFZEQjy-uBkY",
        authDomain: "angulartut-8f382.firebaseapp.com",
        databaseURL: "https://angulartut-8f382.firebaseio.com",
        storageBucket: "angulartut-8f382.appspot.com",
      };
      firebase.initializeApp(config);
    </script>

    <script src="js/angularfire.min.js"></script>

    <script type="text/javascript" src="js/lib/app.js"></script>
    <script type="text/javascript" src="js/controllers/registration.js"></script>
    <script type="text/javascript" src="js/controllers/success.js"></script>
</head>
<body>
<header>
    <nav class="cf" ng-include="'views/nav.html'"></nav>
</header>
<div class="page" ng-view>
    <main></main>
</div>

</body>
</html>

app.js:

var myApp = angular.module('myApp', ['ngRoute', 'firebase']);

myApp.config(['$routeProvider', function($routeProvider) {
   ...
}]);

registration.js:

myApp.controller('RegistrationController', ['$scope','$firebaseAuth', function($scope, $firebaseAuth){
    // $scope.message = "Welcome to my App!";
    console.log($firebaseAuth);
    var ref = new Firebase('https://angulartut-8f382.firebaseio.com');

    // var ref = firebase.database().ref(); //<--tried this, but get error 'onAuth is not a function'
    var auth = $firebaseAuth(ref);

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

    };

    $scope.register = function() {
        auth.$createUser({
            email:$scope.user.email,
            password:$scope.user.password
        }).then(function(regUser) {
            $scope.message = "Welcome " + $scope.user.firstname + "thanks for registering!";
        }).catch(function(error) {
            $scope.message = error.message;
        });

    };
}]);

1 个答案:

答案 0 :(得分:4)

我找到了一个google小组https://groups.google.com/forum/#!forum/firebase-angular,引导我进入此迁移页面https://github.com/firebase/angularfire/blob/master/docs/migration/1XX-to-2XX.md

如您所见,移动配置并更改ref和auth行以及auth。$ createUser()。谢谢你的期待!

以下是registration.js的固定代码:

var config = {apiKey: "AIzaSyCujdl_kIpR42CEqfgC2HxXFZEQjy-uBkY",
            authDomain: "angulartut-8f382.firebaseapp.com",
            databaseURL: "https://angulartut-8f382.firebaseio.com",
            storageBucket: "angulartut-8f382.appspot.com",
          };
firebase.initializeApp(config);

myApp.controller('RegistrationController', ['$scope','$firebaseAuth', function($scope, $firebaseAuth){
    // $scope.message = "Welcome to my App!";


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

    $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 = "Welcome " + $scope.user.firstname + ", thanks for registering!";
        }).catch(function(error) {
            $scope.message = error.message;
        });

    };
}]);