未捕获的参考错误:未定义Firebase

时间:2016-06-03 14:59:43

标签: javascript html firebase firebase-realtime-database

我有以下代码我试图用来验证新注册的用户,虽然它甚至无法初始化firebase,但是弹出了这个错误:

  

未捕获的ReferenceError:未定义firebase

以下是我的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>register</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
    <script src='https://cdn.firebase.com/js/client/2.4.2/firebase.js'></script>
</head>
<body>
    <script>
        var config = {
            databaseURL: 'https://apcs-4bfaa.firebaseio.com/'
        };
        firebase.initializeApp(config);

        var ref = new Firebase("https://apcs-4bfaa.firebaseio.com/");

        function registerUser() {
            var email = document.getElementById("email").value;
            var password = document.getElementById("password").value;
            ref.auth().createNewUserWithEmailAndPassword(email, password);
        }
    </script>

    <div class="form">
        <div id="error"></div>
        <form onsubmit="registerUser();">
            <label>Email:</label>
            <input type="text" name="email" id="email"><br>
            <label>Password:</label>
            <input type="password" name="pwd" id="password"><br>
            <input type="submit" value="Sign Up"><br>
        </form>
    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:8)

旧版2.4.2 API与3.0 SDK之间的代码混杂在一起。

不再有new Firebase()次来电。您需要使用当前3.0.3的新SDK,然后配置您的应用。

<script src="https://www.gstatic.com/firebasejs/3.0.3/firebase.js"></script>
<script>
  // Initialize Firebase
  var config = {
    apiKey: '<your-api-key>',
    authDomain: '<your-auth-domain>',
    databaseURL: '<your-database-url>',
    storageBucket: '<your-storage-bucket>'
  };
  firebase.initializeApp(config);
</script>

配置完成后,您可以创建参考。

firebase.database().ref()

所以在你的情况下,它将是:

<script>
    var config = {
        databaseURL: 'https://apcs-4bfaa.firebaseio.com/'
    };
    firebase.initializeApp(config);

    // new 3.0 SDK method!
    var ref = firebase.database().ref();

    function registerUser() {
        var email = document.getElementById("email").value;
        var password = document.getElementById("password").value;
        firebase.auth().createNewUserWithEmailAndPassword(email, password);
    }
</script>

Check out the docs for more info.