简体   繁体   English

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

[英]Uncaught Reference Error: Firebase is not defined

I have the following piece of code I'm trying to use to authenticate a newly registered user, although it fails to even initialize firebase, popping up this error: 我有以下代码我试图用来验证新注册的用户,虽然它甚至无法初始化firebase,弹出这个错误:

Uncaught ReferenceError: firebase is not defined 未捕获的ReferenceError:未定义firebase

Below is my code: 以下是我的代码:

<!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>

Your code is mixed up between the older 2.4.2 API and the the 3.0 SDK. 您的代码在较旧的2.4.2 API和3.0 SDK之间混淆。

There are no more new Firebase() calls. 没有new Firebase()调用。 You need to use the new SDK, currently 3.0.3 , and then configure your app. 您需要使用当前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>

After configuring you can then create references. 配置完成后,您可以创建引用。

firebase.database().ref()

So in your case it would be: 所以在你的情况下它将是:

<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. 查看文档以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM