[英]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.