简体   繁体   English

如何解决这个 firebase is not defined 错误?

[英]how to solve this firebase is not defined error?

I'm new to firebase/javascript/html coding, so I really don't know what I'm doing.我是 firebase/javascript/html 编码的新手,所以我真的不知道自己在做什么。 But I was following a tutorial on how to make firebase work with a WebGL Unity build and got stuck here:但是我正在学习如何使 firebase 与 WebGL Unity 构建一起工作的教程并卡在这里:

html code ` html代码`

<script type="module">
          // Import the functions you need from the SDKs you need
          import { initializeApp } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-app.js";
          import { getDatabase } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-database.js";
          // Your web app's Firebase configuration
          // For Firebase JS SDK v7.20.0 and later, measurementId is optional
          const firebaseConfig = {
              apiKey: "apikey",
              authDomain: "authDomain.firebaseapp.com",
              databaseURL: "databaseURL.firebasedatabase.app",
              projectId: "projectId",
              storageBucket: "storageBucket.appspot.com",
              messagingSenderId: "messagingSenderId",
              appId: "appId",
              measurementId: "G-measurementId"
          };
          // Initialize Firebase
          const app = initializeApp(firebaseConfig);
          const db = getDatabase(app);
      </script>

jslib script:

PostJSON: function(path, value, objectName, callback, fallback) {
        var parsedPath = Pointer_stringify(path);
        var parsedValue = Pointer_stringify(value);
        var parsedObjectName = Pointer_stringify(objectName);
        var parsedCallback = Pointer_stringify(callback);
        var parsedFallback = Pointer_stringify(fallback);

        try {

            firebase.database().ref(parsedPath).set(JSON.parse(parsedValue)).then(function(unused) {
                unityInstance.Module.SendMessage(parsedObjectName, parsedCallback, "Success: " + parsedValue + " was posted to " + parsedPath);
            });

        } catch (error) {
            unityInstance.Module.SendMessage(parsedObjectName, parsedFallback, JSON.stringify(error, Object.getOwnPropertyNames(error)));
        }
    }

` `

Every time I try to post a value it says: firebase is not defined.每次我尝试发布一个值时,它都会说:未定义 firebase。 What am I doing wrong?我究竟做错了什么?

You're mixing the namespaced firebase.database() syntax of Firebase SDK versions 8 and before with the modular initializeApp syntax of Firebase SDK versions 9 and later, which doesn't work.您将 Firebase SDK 版本 8 及之前的命名空间firebase.database()语法与 Firebase SDK 版本 9 及更高版本的模块化initializeApp语法混合在一起,这是行不通的。

Since you're initializing the modular SDK, your database access code should use the new modular syntax too:由于您正在初始化模块化 SDK,因此您的数据库访问代码也应使用新的模块化语法:

import { getDatabase, ref, set } from "firebase/database";

const db = getDatabase();
set(ref(db, parsedPath), JSON.parse(parsedValue)).then(function(unused) {
    unityInstance.Module.SendMessage(parsedObjectName, parsedCallback, "Success: " + parsedValue + " was posted to " + parsedPath);
});

To learn how to upgrade the code, have a look at the migration guide and keep the Firebase documentation handy as all code samples in there have variants for both syntaxes such as in this page on reading and writing data .要了解如何升级代码,请查看迁移指南并随身携带 Firebase 文档,因为其中的所有代码示例都有两种语法的变体,例如本页关于读取和写入数据的内容。

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

相关问题 如何解决启动时出现 Firebase CLI 错误 - how to solve Firebase CLI error on start up 如何解决:错误:无法列出 Firebase 项目。 有关更多信息,请参阅 firebase-debug.log - How do I solve: Error: Failed to list Firebase projects. See firebase-debug.log for more info 如何解决您的 function 被杀死是因为它在我的 Firebase 模拟器中引发了未处理的错误? - how to solve Your function was killed because it raised an unhandled error in my Firebase emulator? 如何解决{错误:'MismatchSenderId'}? - How to solve { error: 'MismatchSenderId' }? 如何解决 Key 中的 FireBase 数据库禁用字符或解决方法 - How can I solve FireBase database forbidden characters in Key or a workaround 在 firebase 中部署后我的网站没有显示如何解决这个问题 - My website is not showing after deploy in firebase how to solve this Javascript:Firebase 未定义 - Javascript: Firebase is not defined 未捕获的引用错误:未定义 Firebase - Uncaught ReferenceError: Firebase is not defined 未捕获的 ReferenceError:未定义 firebase - Uncaught ReferenceError: firebase is not defined 如何解决 Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app) - How to solve Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM