2016-07-22 104 views
3

我正在嘗試在Angular和Firebase上做一個Lynda.com教程,但我遇到了麻煩。我發現這個錯誤有幾頁,但仍然無法弄清楚。正如在另一個頁面上指出的那樣,我嘗試將firebase.database()。ref()換成'new Firebase()',但它給出了一個不同的錯誤。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; 
     }); 

    }; 
}]); 
+0

您是否嘗試過與添加'angularfire.js' https://www.firebase.com/docs/web/libraries/angular/quickstart。 html – agriboz

+0

因此,在查看https://firebase.google.com/support/guides/firebase-web後,我將config放入registration.app中,並換出新的Firebase()。我現在收到錯誤消息:'$ firebaseAuth服務接受一個Firebase身份驗證實例(或者什麼也沒有),而不是數據庫引用。「在auth = $ firebaseAuth行上。 –

+0

agriboz,我做了沒有效果的變化。 –

回答

4

我發現一個谷歌組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; 
     }); 

    }; 
}]); 
+0

我無法註冊,在漫遊了兩個小時後,找到了你的答案。這ans幫助了我。 –

相關問題