ReferenceError:未在Javascript中定义firebase

ReferenceError:未在Javascript中定义firebase,javascript,firebase,Javascript,Firebase,我设计了一个登录(通过firebase)页面。成功登录后,我想访问主页。但终端显示此错误(ReferenceError:firebase未定义)。根据我的意见,此错误发生在firebase数据库初始化中。任何人请帮助我消除此错误 <script src="https://www.gstatic.com/firebasejs/6.6.2/firebase-app.js"></script> </head> <body> <h1

我设计了一个登录(通过firebase)页面。成功登录后,我想访问主页。但终端显示此错误(ReferenceError:firebase未定义)。根据我的意见,此错误发生在firebase数据库初始化中。任何人请帮助我消除此错误

    <script src="https://www.gstatic.com/firebasejs/6.6.2/firebase-app.js"></script>
</head>
<body>

    <h1> You are Logged in!!! </h1>

    <script src="js/app.js"></script>

    <script>
        firebase.auth().onAuthStateChanged(function(user){
            if(!user){
                window.location.href = "index.html";
            }
        });
    </script>
</body>
这是我的index.html:

<!DOCTYPE html>
<html>
<head>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E=" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="css/style.css" />

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

</head>
<body class="bg-dark">

    <div id="login-card" class="card">
        <div class="card-body">
            <h1>Wallpaper App Admin</h1>
            <form id="login-form">
                <div class="form-group">
                    <label for="email">email</label>
                    <input type="email" id="email" class="form-control"   />
                </div>
                <div class="form-group">
                        <label for="password">Password</label>
                        <input type="password" id="password" class="form-control" />
                </div>
                <div class="form-group">
                    <button id="btn-login" type="button" class="btn btn-primary">Login</button>
                </div>
            </form>
        </div>
    </div>

    <script src="js/app.js"></script>

    <script>

        firebase.auth().onAuthStateChanged(function(user){
            if(user){
                window.location.href = "admin.html";
            }
        });
    </script>
</body>
</html>
    <script src="https://www.gstatic.com/firebasejs/6.6.2/firebase-app.js"></script>
</head>
<body>

    <h1> You are Logged in!!! </h1>

    <script src="js/app.js"></script>

    <script>
        firebase.auth().onAuthStateChanged(function(user){
            if(!user){
                window.location.href = "index.html";
            }
        });
    </script>
</body>
这是我的调试控制台:

    <script src="https://www.gstatic.com/firebasejs/6.6.2/firebase-app.js"></script>
</head>
<body>

    <h1> You are Logged in!!! </h1>

    <script src="js/app.js"></script>

    <script>
        firebase.auth().onAuthStateChanged(function(user){
            if(!user){
                window.location.href = "index.html";
            }
        });
    </script>
</body>
c:\Users\hp\Desktop\WallpaperAppAdmin\public\js\app.js:11
firebase.initializeApp(firebaseConfig);
^

ReferenceError: firebase is not defined
at Object.<anonymous> (c:\Users\hp\Desktop\WallpaperAppAdmin\public  \js\app.js:11:3)
at Module._compile (internal/modules/cjs/loader.js:775:14)

ReferenceError: firebase is not defined
at Object.<anonymous> (c:\Users\hp\Desktop\WallpaperAppAdmin\public \js\app.js:11:3)
 at Module._compile (internal/modules/cjs/loader.js:775:14)
当我在app.js文件的开头添加Import语句时,它会显示在调试控制台中:

    <script src="https://www.gstatic.com/firebasejs/6.6.2/firebase-app.js"></script>
</head>
<body>

    <h1> You are Logged in!!! </h1>

    <script src="js/app.js"></script>

    <script>
        firebase.auth().onAuthStateChanged(function(user){
            if(!user){
                window.location.href = "index.html";
            }
        });
    </script>
</body>
Debugger attached.
c:\Users\hp\Desktop\WallpaperAppAdmin\public\js\app.js:1
import firebase from 'firebase'

请指导我如何编辑这些代码行以消除此错误。请提前感谢。

您需要导入firebase,如果不导入它,您将无法使用firebase

    <script src="https://www.gstatic.com/firebasejs/6.6.2/firebase-app.js"></script>
</head>
<body>

    <h1> You are Logged in!!! </h1>

    <script src="js/app.js"></script>

    <script>
        firebase.auth().onAuthStateChanged(function(user){
            if(!user){
                window.location.href = "index.html";
            }
        });
    </script>
</body>
如果您正在使用node/express use:npm i firebase,请在app.js中导入firebase。 那么这个错误就会消失

    <script src="https://www.gstatic.com/firebasejs/6.6.2/firebase-app.js"></script>
</head>
<body>

    <h1> You are Logged in!!! </h1>

    <script src="js/app.js"></script>

    <script>
        firebase.auth().onAuthStateChanged(function(user){
            if(!user){
                window.location.href = "index.html";
            }
        });
    </script>
</body>
从“firebase”导入firebase
var firebaseConfig={
apiKey:“Aizasydjjmzbv8ebrz_oEctHiwor84wlhLPXJKoI”,
authDomain:“e-agriculture9.firebaseapp.com”,
数据库URL:“https://e-agriculture9.firebaseio.com",
项目D:“电子农业9”,
storageBucket:“”,
messagingSenderId:“1086965307452”,
appId:“1:1086965307452:web:ea0fd9076744c723a5cb39”
};
//初始化Firebase
firebase.initializeApp(firebaseConfig);
firebase.auth.auth.Persistence.LOCAL;
$(“#btn登录”)。单击(函数(){
警告(“你点击了这里!”);
var email=$(“#email”).val();
var password=$(“#password”).val();
var result=firebase.auth();
result.catch(函数(错误){
var errorCode=error.code;
var errorMessage=error.message;
控制台日志(错误代码);
控制台日志(错误消息);
});

});感谢您真诚的关注。你的建议对我非常有效。@JavedAbrar你好,请接受答案,因为它为你提供了问题的解决方案。在你的帖子中只看到一个带有
apiKey
的提示,如果这是真正的密钥,让它公开会给你带来不幸。