问题
I'm working with Firebase for the first time on a practice project and I'm having a very difficult time setting up the ability for users to log in to their accounts.
I've successfully set up registration but so far I can't get log in and check auth state to work properly.
The error I keep receiving in the console is "Uncaught ReferenceError: Firebase is not defined"
I've done some research on my own but the only answers I seem to find are ones saying you need to include the script tags for Firebase, which isn't relevant here because I have included them, or outdated responses from version 2.4.2
<head>
<script src="https://www.gstatic.com/firebasejs/3.0.5/firebase.js"></script>
<script>
/*global Firebase */
// Initialize Firebase
var config = {
apiKey: "omit",
authDomain: "omit",
databaseURL: "omit",
storageBucket: "omit",
};
firebase.initializeApp(config);
</script>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js'></script>
The code in question is here:
// Create a callback which logs the current auth state
var ref = new Firebase("omit");
ref.onAuth(function(authData) {
if (authData) {
console.log("User " + authData.uid + " is logged in with " + authData.provider);
} else {
console.log("User is logged out");
}
});
回答1:
As Frank van Puffelen pointed out, and as per the Firebase documentation, in Firebase 3.x, the root database reference is instantiated as firebase.database().ref()
and not as new Firebase("<path>");
. This should solve the uncaught reference error.
Fixing it will, of course, be followed by other errors in your code, as ALI MAKEEN pointed out. Refer the latest documentation. BTW, if you're using AngularJS, AngularFire is an excellent binding for Firebase, and it supports user authentication.
来源:https://stackoverflow.com/questions/37901409/uncaught-referenceerror-firebase-is-not-defined