【问题标题】:Firebase (Firestore) Javascript Keep showing error [closed]Firebase(Firestore)Javascript不断显示错误[关闭]
【发布时间】:2021-05-14 18:26:20
【问题描述】:

卡了好几天了。 代码实际上是从 google firebase 文档中复制而来的。

我想要的只是在 firebase 控制台中的 firestore 上写入和读取数据。 Firebase (Firestore) Javascript 一直显示错误。

谁能帮我写代码?我不需要花哨的代码或其他任何东西。只需在 Firestore 中写入和读取即可。

<!DOCTYPE html>
<html>
<head>
  <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-firestore.js"></script>


</head>
<body>
  <script>
    // Firebase App (the core Firebase SDK) is always required and must be listed first
import firebase from "firebase/app";
// If you are using v7 or any earlier version of the JS SDK, you should import firebase using namespace import
// import * as firebase from "firebase/app"

// If you enabled Analytics in your project, add the Firebase SDK for Analytics
import "firebase/analytics";

// Add the Firebase products that you want to use
import "firebase/auth";
import "firebase/firestore";

// TODO: Replace the following with your app's Firebase project configuration
// For Firebase JavaScript SDK v7.20.0 and later, `measurementId` is an optional field
const firebaseConfig = {
    apiKey: "******",
    authDomain: "retest-5308a.firebaseapp.com",
    databaseURL: "https://retest-5308a-default-rtdb.firebaseio.com",
    projectId: "retest-5308a",
    storageBucket: "retest-5308a.appspot.com",
    messagingSenderId: "125780245080",
    appId: "1:125780245080:web:530259dc2a4b1362aae82d",
    measurementId: "G-PNTZS15X9K"
  };
  
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);

const firebase = require("firebase");
// Required for side-effects
require("firebase/firestore");

// Initialize Cloud Firestore through Firebase
firebase.initializeApp({
    apiKey: "********",
    authDomain: "retest-5308a.firebaseapp.com",
    projectId: "retest-5308a"
  });
  
  var db = firebase.firestore();

  db.collection("users").add({
    first: "Ada",
    last: "Lovelace",
    born: 1815
})
.then((docRef) => {
    console.log("Document written with ID: ", docRef.id);
})
.catch((error) => {
    console.error("Error adding document: ", error);
});
  </script>
</body>

</html>

【问题讨论】:

  • 能否请您至少发布错误?
  • 好像有多个问题。当我修复其中一些时,错误会从其他地方弹出。请查看此网址。 retest-5308a.firebaseapp.com

标签: javascript firebase google-cloud-firestore


【解决方案1】:

您不能在 HTML 页面的 script 标记内使用 import firebase from "firebase/app";

相反,您应该通过它们自己的 script 标签导入这些 SDK,就像您已经为 &lt;script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-app.js"&gt;firebase-firestore.js 所做的那样。

一旦您通过script 标签添加了SDK,就不再需要imports

【讨论】:

  • 非常感谢。我删除了导入和要求,它运行良好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-08
  • 2018-10-12
  • 1970-01-01
相关资源
最近更新 更多