Firebase: Uncaught SyntaxError: Invalid or unexpected token -> uncaught referenceError
Firebase: Uncaught SyntaxError: Invalid or unexpected token -> uncaught referenceError
在我的网络应用程序中,我正在实施 Firebase 的 Google 登录授权,但在网站加载时看到 SyntaxError
,在按钮 onclick
[= 上看到 referenceError
22=]
我的 html 代码包含一个登录按钮和 Firebase 脚本:
<button onclick="login()">Sign In</button>
<script src="https://www.gstatic.com/firebasejs/7.17.1/firebase-app.js"></script>
<!-- TODO: Add SDKs for Firebase products that you want to use
https://firebase.google.com/docs/web/setup#available-libraries -->
<script src="https://www.gstatic.com/firebasejs/7.17.1/firebase-analytics.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.17.1/firebase-auth.js"></script>
我删除了下面的特定键和 ID 以使问题更笼统,但它们正确地绑定到 Firebase 项目。
<script>
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "key",
authDomain: "mywebsite.firebaseapp.com",
databaseURL: "https://mywebsite.firebaseio.com",
projectId: "signup",
storageBucket: "signup.appspot.com",
messagingSenderId: "senderId",
appId: "appId",
measurementId: "measureId"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
firebase.analytics();
</script>
我正在链接到一个 JS 文件 google_sign_in.js
(这是我的文件结构中的正确路径)
<script src="../static/js/google_sign_in.js" type="text/javascript"></script>
我的google_sign_in.js代码如下:
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
console.log("user signed in")
} else {
// No user is signed in.
console.log("user signed out")
}
});
var provider = new firebase.auth.GoogleAuthProvider();
function login() {
firebase.auth().signInWithPopup(provider).then(function(result) {
// This gives you a Google Access Token. You can use it to access the Google API.
var token = result.credential.accessToken;
// The signed-in user info.
var user = result.user;
// ...
}).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
// ...
});
}
// sign signOut
function signOut() {
// Sign out of Firebase.
firebase.auth().signOut();
}
var user = firebase.auth().currentUser;
if (user != null) {
user.providerData.forEach(function(profile) {
console.log("Sign-in provider: " + profile.providerId);
console.log(" Provider-specific UID: " + profile.uid);
console.log(" Name: " + profile.displayName);
console.log(" Email: " + profile.email);
console.log(" Photo URL: " + profile.photoURL);
});
}
更新 - 开发工具的新 javascript 代码和错误截图:
firebase.auth().onAuthStateChanged(function(user) {
if (user) { // User is signed in.
console.log("user signed in")
} else {
// No user is signed in.
console.log("user signed out")
}
});
var provider = new firebase.auth.GoogleAuthProvider();
function login() {
firebase.auth().signInWithPopup(provider).then(function(result) {
// This gives you a Google Access Token. You can use it to access the Google API.
var token = result.credential.accessToken;
// The signed-in user info.
var user = result.user;
// ...
}).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
// ...
});
} // sign signOut
function signOut() {
// Sign out of Firebase.
firebase.auth().signOut();
}
var user = firebase.auth().currentUser;
if (user != null) {
user.providerData.forEach(function(profile) {
console.log("Sign-in provider: " + profile.providerId);
console.log(" Provider-specific UID: " + profile.uid);
console.log(" Name: " + profile.displayName);
console.log(" Email: " + profile.email);
console.log(" Photo URL: " + profile.photoURL);
});
}
好吧,这是一个艰难的过程...
您的代码中有一些偷偷摸摸的隐藏字符(\u200b
- 零宽度 space),可能是由于从网页复制和粘贴所致。
在你的文本编辑器中使用方向键,你会看到光标跳过这些。删除它们。
在我的网络应用程序中,我正在实施 Firebase 的 Google 登录授权,但在网站加载时看到 SyntaxError
,在按钮 onclick
[= 上看到 referenceError
22=]
我的 html 代码包含一个登录按钮和 Firebase 脚本:
<button onclick="login()">Sign In</button>
<script src="https://www.gstatic.com/firebasejs/7.17.1/firebase-app.js"></script>
<!-- TODO: Add SDKs for Firebase products that you want to use
https://firebase.google.com/docs/web/setup#available-libraries -->
<script src="https://www.gstatic.com/firebasejs/7.17.1/firebase-analytics.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.17.1/firebase-auth.js"></script>
我删除了下面的特定键和 ID 以使问题更笼统,但它们正确地绑定到 Firebase 项目。
<script>
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "key",
authDomain: "mywebsite.firebaseapp.com",
databaseURL: "https://mywebsite.firebaseio.com",
projectId: "signup",
storageBucket: "signup.appspot.com",
messagingSenderId: "senderId",
appId: "appId",
measurementId: "measureId"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
firebase.analytics();
</script>
我正在链接到一个 JS 文件 google_sign_in.js
(这是我的文件结构中的正确路径)
<script src="../static/js/google_sign_in.js" type="text/javascript"></script>
我的google_sign_in.js代码如下:
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
console.log("user signed in")
} else {
// No user is signed in.
console.log("user signed out")
}
});
var provider = new firebase.auth.GoogleAuthProvider();
function login() {
firebase.auth().signInWithPopup(provider).then(function(result) {
// This gives you a Google Access Token. You can use it to access the Google API.
var token = result.credential.accessToken;
// The signed-in user info.
var user = result.user;
// ...
}).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
// ...
});
}
// sign signOut
function signOut() {
// Sign out of Firebase.
firebase.auth().signOut();
}
var user = firebase.auth().currentUser;
if (user != null) {
user.providerData.forEach(function(profile) {
console.log("Sign-in provider: " + profile.providerId);
console.log(" Provider-specific UID: " + profile.uid);
console.log(" Name: " + profile.displayName);
console.log(" Email: " + profile.email);
console.log(" Photo URL: " + profile.photoURL);
});
}
更新 - 开发工具的新 javascript 代码和错误截图:
firebase.auth().onAuthStateChanged(function(user) {
if (user) { // User is signed in.
console.log("user signed in")
} else {
// No user is signed in.
console.log("user signed out")
}
});
var provider = new firebase.auth.GoogleAuthProvider();
function login() {
firebase.auth().signInWithPopup(provider).then(function(result) {
// This gives you a Google Access Token. You can use it to access the Google API.
var token = result.credential.accessToken;
// The signed-in user info.
var user = result.user;
// ...
}).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
// ...
});
} // sign signOut
function signOut() {
// Sign out of Firebase.
firebase.auth().signOut();
}
var user = firebase.auth().currentUser;
if (user != null) {
user.providerData.forEach(function(profile) {
console.log("Sign-in provider: " + profile.providerId);
console.log(" Provider-specific UID: " + profile.uid);
console.log(" Name: " + profile.displayName);
console.log(" Email: " + profile.email);
console.log(" Photo URL: " + profile.photoURL);
});
}
好吧,这是一个艰难的过程...
您的代码中有一些偷偷摸摸的隐藏字符(\u200b
- 零宽度 space),可能是由于从网页复制和粘贴所致。
在你的文本编辑器中使用方向键,你会看到光标跳过这些。删除它们。