繁体   English   中英

Firebase:未捕获的语法错误:无效或意外的令牌 -> 未捕获的引用错误

[英]Firebase: Uncaught SyntaxError: Invalid or unexpected token -> uncaught referenceError

在我的网络应用程序中,我正在实施 Firebase 的 Google 登录授权,但在网站加载时看到SyntaxError ,并且在按钮上看到referenceError onclick

在此处输入图片说明

我的 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 代码和 devtools 的错误截图:

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);
  });
}

在此处输入图片说明

好吧,这是一个艰难的...

您的代码中有一些偷偷摸摸的隐藏字符( \​ - 零宽度空间),可能是由于从网页复制和粘贴。

使用文本编辑器中的箭头键,您将看到光标跳过这些。 删除它们。

隐形字符

暂无
暂无

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

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