您好我正在嘗試在我的網站中實作 firebase 實時資料庫 API,我正在關注此檔案:
這是我的代碼:
<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";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.8.3/firebase-analytics.js";
import { getDatabase } from "https://www.gstatic.com/firebasejs/9.8.3/firebase-database.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
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: "xxxxxxxxxxxxxxxxxxxxx",
authDomain: "xx.x.xxx",
databaseURL: "xxxxxxxxxxxxxxxx",
projectId: "xxxxx",
storageBucket: "xxxxxxxxx",
messagingSenderId: "xxxx",
appId: "xxxxx",
measurementId: "xxxxxxx"
};
const fireapp = initializeApp(firebaseConfig);
const db = getDatabase(fireapp);
const ref = db.ref('server/saving-data/fireblog');
</script>
我究竟做錯了什么?我使用的版本可能不正確嗎?
uj5u.com熱心網友回復:
您將 API 的新模塊化/v9 語法與舊的命名空間語法混合在一起,這是行不通的。
在 v9 中,最后一行的等價物是:
const dbref = ref(db, 'server/saving-data/fireblog');
由于您似乎在使用過時的代碼,因此我建議您將檔案放在手邊(例如,獲取參考的本節)以比較 v8 和 v9 代碼示例,并閱讀升級指南。
uj5u.com熱心網友回復:
您正在使用Firebase 客戶端 SDK ,但請參閱尚未完全模塊化的Firebase Admin SDK檔案。嘗試使用ref()函式重構代碼:
import { ref } from "firebase/database"
const dbRef = ref(db, 'server/saving-data/fireblog');
查看Firebase JS SDK(選項卡)的檔案以Modular獲取正確的語法。
uj5u.com熱心網友回復:
似乎您正在使用 NodeJS 后端 SDK 的檔案。您的前端開發起點應該是firebase docs – web start。
對于您包含在網站中的 Web-SDK v9,有不同的功能可供使用。查看firebase 檔案——使用它們的網路讀/寫。
幾周前,當我第一次使用 firebase 進行 Web 開發時,我也很困惑。我希望它有所幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/489926.html
標籤:javascript 火力基地 firebase-实时数据库
