繁体   English   中英

为什么我无法将 javascript 与 firebase 连接?

[英]why i can't connect javascript with firebase?

我试图将我的javascript应用程序与firebase连接,我在firebase中创建了一个帐户,我粘贴了我的配置,但它给了我3个错误:

意外的令牌“出口”;

不能在模块外使用 import 语句;

firebase 未定义

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <script src="https://www.gstatic.com/firebasejs/9.8.3/firebase-app.js"></script> <script src="https://www.gstatic.com/firebasejs/9.8.3/firebase-firestore.js"></script> <script type="module"> // Import the functions you need from the SDKs you need import { initializeApp } from "https://www.gstatic.com/firebasejs/9.8.3/firebase-app.js"; // TODO: Add SDKs for Firebase products that you want to use // https://firebase.google.com/docs/web/setup#available-libraries // Your web app's Firebase configuration const firebaseConfig = { apiKey: "<key>", authDomain: "javascript-3bbaa.firebaseapp.com", projectId: "javascript-3bbaa", storageBucket: "javascript-3bbaa.appspot.com", messagingSenderId: "<id>", appId: "<appId>" }; // Initialize Firebase const firebase = initializeApp(firebaseConfig); const db = firebase.firestore(); </script> </body> </html>

在 firebase v9 中,您必须使用 getFirestore 方法。 尝试这个:

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <script type="module"> import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.8.3/firebase-app.js'; import { getFirestore, collection, getDocs } from 'https://www.gstatic.com/firebasejs/9.8.3/firebase-firestore-lite.js'; const firebaseConfig = { apiKey: "<key>", authDomain: "javascript-3bbaa.firebaseapp.com", projectId: "javascript-3bbaa", storageBucket: "javascript-3bbaa.appspot.com", messagingSenderId: "<id>", appId: "<appId>" }; // Initialize Firebase const app = initializeApp(firebaseConfig); const db = getFirestore(app); </script> </body> </html>

暂无
暂无

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

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