我花了好幾個小時在這里搜索,但沒有任何作業要做。我想使用 firebase 實時資料庫,但無論我嘗試什么,我都會不斷出錯。這個 webapp 的目的是從串列中添加、編輯、查看和洗掉產品。到目前為止,這就是我在 data.js 中的內容。任何幫助,將不勝感激 :)
import firebase from 'firebase/compat/app';
import store from "./store";
import 'firebase/compat/auth';
import 'firebase/compat/firestore';
import 'firebase/database';
import { getFirestore } from "firebase/firestore";
import { initializeApp } from "firebase/app";
const firebaseConfig = {
apiKey: "",
authDomain: "",
projectId: "",
databaseURL: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
measurementId: ""
};
const app = initializeApp(firebaseConfig);
const database = getFirestore();
export default function setListingData() {
database.ref("/listings")
.get()
.then(function(snapshot) {
if (snapshot.exists()) {
let listings = [];
snapshot.forEach((e) => {
listings.push(e.val());
});
console.log(listings);
store.commit("initListings", listings);
return snapshot.val();
} else {
console.log("No data available");
}
})
.catch(function(error) {
console.error(error);
});
}
export function deleteListing(id) {
firebase
.database()
.ref(`/listings/${id}`)
.remove();
}
/**
* Add/edit listing
* @param {*} listing The listing
*/
export function addListing(listing) {
console.log("ADDING:", listing);
firebase
.database()
.ref(`/listings/${listings.id}`)
.set(listings);
}
export function emptyListing() {
return {
title: "",
price: "",
description: ""
};
}
uj5u.com熱心網友回復:
您在這里混合了 Firestore 和實時資料庫。雖然這兩個資料庫都是 Firebase 的一部分,但它們是完全獨立的,并且每個都有自己的 API。
如關于開始使用實時資料庫的檔案和關于使用兼容庫的部分中所示,您將獲得一個資料庫實體:
import firebase from 'firebase/compat/app';
import store from "./store";
import 'firebase/compat/auth';
import 'firebase/compat/database'; // ??
// ?? Remove all Firestore imports, and all fine-grained improts
接著
firebase.initializeApp(firebaseConfig);
// Get a reference to the database service
var database = firebase.database();
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/486758.html
