【问题标题】:Getting firebase is not defined on setting document in html/ javascript在 html/javascript 中设置文档时未定义获取 firebase
【发布时间】:2021-09-15 13:16:04
【问题描述】:

所以,基本上我正在尝试将数据保存在 firebase 中,但出现错误“未定义 firebase” 我的代码:-

import { initializeApp } from "https://www.gstatic.com/firebasejs/9.0.1/firebase-app.js";
      const firebaseConfig = {
        apiKey: "",
        authDomain: "",
        projectId: "",
        storageBucket: "",
        messagingSenderId: "",
        appId: ""
      };
    
      // Initialize Firebase
      const app = initializeApp(firebaseConfig);
      firebase                                                  // getting error over here
  .firestore()
  .collection("books")
  .add({
    title: "Of Mice and Men",
  })
  .then((ref) => {
    console.log("Added doc with ID: ", ref.id);
    // Added doc with ID:  ZzhIgLqELaoE3eSsOazu
  });

【问题讨论】:

    标签: javascript html google-cloud-firestore


    【解决方案1】:

    自其 JavaScript SDK 第 9 版以来,Firebase 已切换到新的模块化语法,您可以在其中导入单个函数。您必须选择是要使用新语法,还是通过其兼容层继续使用以前的语法。

    import the compatibility libraries,使用:

    // v9 compat packages are API compatible with v8 code
    import firebase from 'firebase/compat/app';
    import 'firebase/compat/auth';
    import 'firebase/compat/firestore';
    

    若要切换到新语法,请按照同一 upgrade guide 中的说明以及其余文档中 code samples 的 v9 选项卡中的说明进行操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-12
      • 2021-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-01
      • 1970-01-01
      • 2022-01-18
      相关资源
      最近更新 更多